简体   繁体   中英

Binding Reset In Backbone.js

I'm trying to do a simple collection in backbone.js, but I don't understand why my callback function (specified in bind) isn't being called. From what I understand from the documentation, when I do a fetch(), the reset event should be triggered. Any suggestions? (Code is below).

            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);

If you are using backbone 1.0.0 you should be passing reset:true to reset. Else it will just set the models to the collection which was the erstwhile update

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

Ensure the response from the url is a valid json. reset event will be fired only on success.

check this out. like @nikoshr said on https://stackoverflow.com/a/16538588/1211174 . there is sync event. so instead forcing backbone to reset the collection each time you can catch the sync

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

    this.collection.bind("sync", function(){
                         console.log('got sync');
                         this.render(){, this  );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM