Parse.com - Parse.Object.extend(...) attributes

Parse.com - Parse.Object.extend(...) attributes

本文关键字:Parse attributes Object com extend      更新时间:2023-09-26

我正在使用Parse JS和Backbone,并尝试打印Parse JS模型中的值列表。这很容易,但我对objectId有问题。

然后,我。。。

var Thing = Parse.Object.extend('MyThings');
var Things = Parse.Collection.extend({
    model: Thing
});
var collection = new Things();
collection.fetch({
    success: function(){
        App.start(collection.toJSON());
    }
});

这是视图。。。

ThingView = Backbone.View.extend({
    tagName: 'tr',
    render: function(){
        this.$el.html(_.template($('#item-template').html(), this.model.attributes));
    }
});
ThingListView = Backbone.View.extend({
    tagName: 'table',
    addAll: function(){
        this.collection.forEach(this.addOne, this);
    },
    render: function(){
        this.$el.empty();
        this.addAll();
    },
    addOne: function(item){
        var itemView = new ThingView({model: item});
        itemView.render();
        this.$el.append(itemView.el);
    }
});

这是一个模板(使用Underscore.js)…

<script type="text/template" id="item-template">
    <td><%= id %></td>
    <td><%= name %></td>
    <td><%= color %></td>
</script>

属性名称和颜色显示正确,但未定义"id'

知道吗?

问题是id不存在于属性中。它实际上生活在物体的根部。因此,您需要做的是将this.model传递到_.template()中,而不是将this.model.attributes,然后在HTML中进行相应调整,例如:

render: function(){
    this.$el.html(_.template($('#item-template').html(), this.model));
}
<td><%= id %></td>
<td><%= attributes.name %></td>
<td><%= attributes.color %></td>