-1

可能重复:
Backbone.js - 添加参数发送以获取不触发集合

在我的函数中,即使我添加了重置以触发我的渲染函数,但什么也没有发生......任何线索我错了我做了什么......?

代码:

$(document).ready(function(){

    var school = {};

    school.model = Backbone.Model.extend({
        defaults:{
            name:'name yet to decide'    
        }    
    });

    school.collect = Backbone.Collection.extend({
        model:school.model,
        url:'js/school.json',
        parse:function(response){
            return response;
        }
    });

    school.view = Backbone.View.extend({
        tagName:'div',
        className:'division',
        template:$('#newTemp').html(),
        render:function(){
            var temp = _.template(this.template);
            this.$el.html(temp(this.model.toJSON()));
            return this;
        }
    });

    school.views = Backbone.View.extend({
        el:$('#content'),
        initialize:function(){
            _.bindAll(this);
            this.collection = new school.collect;
            this.collection.bind("reset", this.render);
            this.collection.fetch({
                update:true
            })
        },
        render:function(){
            var that = this;
            _.each(this.collection.models, function(item){
                that.addItems(newItem);
            })
        },
        resets:function(){
            console.log(this.collection.models)
            console.log('calling from resets')    
        },
        addItems:function(item){
            var newItem = new school.collect({model:item});
            this.$el.append(newItem.render().el);
        },
        changed:function(){
            console.log('calling from changed')
        }
    });

    var newSchool = new school.views;
})
4

1 回答 1

2

与 Backbone.js 1.0.0 一样,您听reset但通过{reset:true}选项。

当模型数据从服务器返回时,集合将(有效)重置,除非您传递 {update: true},在这种情况下,它将使用 update (智能)合并获取的模型

http://backbonejs.org/#Collection-fetch

于 2013-01-28T08:20:55.040 回答