Parse.com检索createdAt字段Javascript

时间:2015-08-02 21:00:56

标签: javascript html backbone.js parse-platform

我在使用parse检索javascript / html中的createdAt字段时遇到问题。这是我在HTML中显示当前字段的方式。作为参考,我从这里得到的原始教程是:http://code.tutsplus.com/tutorials/getting-started-with-parse--net-28000

<script id="todo-items-template" type="x/handlebars">
<table id="data">
<tr><td class="first">Rank</td>
<td class="second"> <li class="list-item"><input type="checkbox" id="{id}">{content}</li></td>
<td class="third" ><li class="list-item"><output type="date" id="{id}">{createdAt}</li></td>
</tr>
</table>

</script>

以下是我如何在javascript中保存它。显示网页时,内容正确,但createdAt字段只显示{createdAt}。正如您在下面看到的那样,我在保存之后尝试直接检索它,但没有运气。我也试过将它放入查询中,也没有运气。我觉得我可能试图错误地显示它。

        //Handle Click Event
        submitBtn.on('click', function(e) {

            //Save the current Todo
            var text = Y.one('#list-input').get('value');
            var ListItem = Parse.Object.extend("ListItem");
            var listItem = new ListItem();


            listItem.set("content", text);
            listItem.set("isComplete", false);
            //Once it is saved, show it in the list of todo's.
            listItem.save(null, {
              success: function(item) {
                    noTasksMessage.addClass('hidden');
                var content = Y.Lang.sub(Y.one('#todo-items-template').getHTML(), {
                        content: item.get('content'),
                        id: item.id,

                    });
                    incompleteItemList.append(content);
                    input.set('value', '').focus();
              },
              error: function(gameScore, error) {
                    alert("Error when saving Todos: " + error.code + " " + error.message);
              }
            });
            var createdAt = listItem.createdAt;
        });

最后,这是我如何检索我的解析对象:

        //Get 10 most recent incomplete Todos.
        ListItem = Parse.Object.extend("ListItem");
        query = new Parse.Query(ListItem);
        query.equalTo('isComplete', false)
        query.limit = 10;
        query.descending('createdAt');
        query.find({
          success: function(results) {
                if (results.length > 0) {
                    noTasksMessage.addClass('hidden');
                }
                //Append each of the incomplete tasks to the Incomplete List
                Y.Array.each(results, function(val, i, arr) {
                    var content = Y.Lang.sub(Y.one('#todo-items-template').getHTML(), {
                        content: val.get('content'),
                        id: val.id,
                        isComplete: false
                    });
                    incompleteItemList.append(content);
                });

                //When the checkbox is clicked for any of the items in the incomplete list, update it as complete.
                incompleteItemList.delegate('click', function (e) {
                    var self = this;
                    query = new Parse.Query(ListItem);
                    query.get(self.one('input').get('id'), {
                      success: function(item) {
                        item.set('isComplete', true);
                            item.save();
                            self.remove();

                            if (incompleteItemList.all('li').size() >= 1) {
                                noTasksMessage.removeClass('hidden');
                            }

                      },
                      error: function(object, error) {
                            alert("Error when updating todo item: " + error.code + " " + error.message);
                      }
                    });
                }, 'li');
          },
          error: function(error) {
            alert("Error when retrieving Todos: " + error.code + " " + error.message);
          }
        });

1 个答案:

答案 0 :(得分:0)

解决它......比我想象的要容易。

首先,在你的html中执行:

<td class="third">{created}</td>

其次,保存时:创建:

item.getDate('createdAt')

最后,在查询时:

created: val.createdAt

瞧!

相关问题