修改骨干中的收集结果

时间:2013-10-22 13:50:30

标签: backbone.js

现在我有一个返回的集合数据,如:

[{'category':1,
     'title':"Current Company", 
     'people':{"count":2,                
             "keyword":"",
             "executiveInfos":[{"name":"Darren Suomi", 
                                "title":"Global Vice President of Sales",
                                "companyId":2867257,
                                "employmentId":10993552,
                                "executiveId":10152454,
                             "imageUrl":"https://d1tzls7byl3ryp.cloudfront.net/iv/profileImages/executiveImage/321446",
                                "count":0,
                                "executiveConnectionImageUrls":[],
                                "trackedByWatchlists":[],
                                "employmentType":0},
                               {"name":"Gregory  Gunn",
                                "title":"Vice President of Business Development",
                                "companyId":2867257,
                                "employmentId":9240823,
                                "executiveId":9049103,
                                "imageUrl":"https://d1tzls7byl3ryp.cloudfront.net/iv/profileImages/executiveImage/292479",
                                "count":0,
                                "executiveConnectionImageUrls":[],
                                "trackedByWatchlists":[],
                                "employmentType":0}]***}
     },
{'category': 2, 
 'title':"Former Company",
 'people':{"count":0,
         "finance":0,
         "otherFunction":0,
         "keyword":"",
         "executiveInfos":[]}
},
{'category': 4,
 'title':"Family Tree Company",
 'people':{"count":0,
         "keyword":"",
         "executiveInfos":[]}
 }
]

在数据中,它有3个类别,在每个类别中它都有一个名为people的属性,现在我 想要列出每个类别中人员的executiveInfos,我的列表视图是:

var PeopleListView =  Backbone.View.extend({
    el: $('.bucketContent'),
    initialize:  function(){
        this.listenTo(this.collection, 'reset', this.render);
    }
        render: function() {
           this.collection.each( function( $model) {                
            var itemview = new PeopleItemView({model : $model});                    
            this.$el.prepend(itemview.render().el);  

        }, this);

        return this;
    }
});

我该怎么办?谢谢。

1 个答案:

答案 0 :(得分:0)

在不知道PeopleItemView做什么的情况下,很难给出正确答案。但是,假设您正确实现此视图,则需要确保正确地将所有模型属性传递给模板。

例如,假设PeopleItemView视图看起来像这样:

var PeopleItemView = Backbone.View.extend({
  this.template = JST["the_template_name"];
  render: function() {
    this.$el.html(this.template(this.model.attributes));
    return this;
  }
});

然后,您可以通过模板本身迭代executiveInfos。在Handlebars中,这看起来像:

{{#each people.executiveInfos}}
    <span>{{name}}</span>
    <span>{{title}}</span>
    <span>{{companyId}}</span>
    <span>{{employmentId}}</span>
    <span>{{executiveId}}</span>
    ...
{{/each}}