1

我试图在backbone.js 中做一个简单的集合,但我不明白为什么我的回调函数(在bind 中指定)没有被调用。根据我从文档中了解到的情况,当我执行 fetch() 时,应该触发重置事件。有什么建议么?(代码如下)。

            Customer = Backbone.Model.extend(
            );
             CustomerList = Backbone.Collection.extend({
                model : Customer,
                url : "test.php",
            });

            var ChartView = Backbone.View.extend({
                el: $('body'),


                initialize: function(){
                    _.bindAll(this, 'render');
                    this.collection.bind("reset", self.render);
                    this.collection.fetch( 
                    );

                },

                render : function() {
                    console.log("render");
                }

            });

            var chartView = new ChartView( { collection: new CustomerList()} );
        })(jQuery);
4

2 回答 2

1

如果您使用的是骨干网 1.0.0,您应该通过reset:true重置。否则,它将只是set以前收藏的模型update

this.collection.fetch({reset:true});

确保来自 url 的响应是有效的 json。reset事件只会在成功时触发。

于 2013-04-06T04:58:29.040 回答
0

看一下这个。就像@nikoshr 在 https://stackoverflow.com/a/16538588/1211174上所说的那样。有同步事件。因此,每次您可以捕获同步时,都会强制骨干网重置集合

https://github.com/backbone-paginator/backbone.paginator/issues/164

    this.collection.bind("sync", function(){
                         console.log('got sync');
                         this.render(){, this  );
于 2013-07-19T10:19:50.203 回答