What events are triggered when calling fetch() on a Backbone.js collection?

后端 未结 3 462
孤城傲影
孤城傲影 2020-12-13 12:07

In my backbone.js app, there is a Trips collection that holds Trip models, which is working with LocalStorage. I am able to call

3条回答
  •  天命终不由人
    2020-12-13 12:32

    As of backbone 1.0, model.fetch() triggers a 'sync'. That's what you should bind to.

    Here's the relevant part from the backbone.js source where the 'sync' event is fired:

    fetch: function(options) {
      options = options ? _.clone(options) : {};
      if (options.parse === void 0) options.parse = true;
      var model = this;
      var success = options.success;
      options.success = function(resp) {
        if (!model.set(model.parse(resp, options), options)) return false;
        if (success) success(model, resp, options);
    
        // HERE'S THE TRIGGER!
        model.trigger('sync', model, resp, options);
    
      };
      wrapError(this, options);
      return this.sync('read', this, options);
    },
    

提交回复
热议问题