Backbone.js synchronization does not cause any events on the model

I have a very simple setup ...

The route is configured, which brings up a modal dialog box using bootstrap. The header of the View calls the method when you click on the menu -

menuClick: function(e){
    e.preventDefault();
    if (!this.myView) {
        this.myView= new MyView({model: new MyModel()});
    }
    this.myView.render();
},

In MyView, I call bind on initialization

initialize: function(){
    this.model.bind('sync', function(model){ alert('test view')});
}

And call Backbone.sync in a button click event:

var response = Backbone.sync('read', this.model, {
    success: function(data, textStatus, jqXHR) { alert('success'); },
    error: function(data, textStatus, jqXHR){ alert(fail); }
});

A warning inside synchronization is raised ... but a warning in the bind command in initialization is never raised. I tried to move the binding inside the model, pulled it out, also tried synchronization: fail, sync: done. No success.

+5
source share
2 answers

, . success error , .

Backbone.sync save, create, fetch receive success error , , , .

Model.save, Model.destroy ..

, , , Backbone.sync , , Model.fetch().

+4

Backbone.sync :

var sync = Backbone.sync;
Backbone.sync = function (method, model, options) {
    var success = options.success;
    options.success = function (resp, status, xhr) {
        //Your logic goes here
        console.log('succeed');
        if (success) success(resp, status, xhr);
    };
    options.error = function (xhr, ajaxOptions, thrownError) {
        console.log('failed');
    }
    sync(method, model, options);
};
+2
source

All Articles