首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Parse.com -Parse.Object.extend(…)属性

Parse.com -Parse.Object.extend(…)属性
EN

Stack Overflow用户
提问于 2014-10-28 12:39:56
回答 1查看 1.5K关注 0票数 2

我正在使用Parse和主干,我尝试从Parse模型中打印一个值列表。这很简单,但我对objectId有问题。

那我有..。

代码语言:javascript
复制
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());
    }
});

这是风景..。

代码语言:javascript
复制
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).

代码语言:javascript
复制
<script type="text/template" id="item-template">
    <td><%= id %></td>
    <td><%= name %></td>
    <td><%= color %></td>
</script>

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

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-28 17:50:15

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

代码语言:javascript
复制
render: function(){
    this.$el.html(_.template($('#item-template').html(), this.model));
}

<td><%= id %></td>
<td><%= attributes.name %></td>
<td><%= attributes.color %></td>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26608608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档