2011-12-30 24 views

回答

2

另一种方式来做到这一点是创建一个基础模型和收藏,让你的模型和集合扩展这些替代骨干:

var BaseModel = Backbone.Model.extend({ 
    onSyncError: function(model, response) { 
    // your error-handling code 
    }, 

    onSyncSuccess: function(model, response) { 
    // do stuff if successful 
    }, 

    // Backbone will call your 'sync' if it exists 
    sync: function(method, model, options) { 
    options.error = this.onSyncError; 
    options.success = this.onSyncSuccess; 
    Backbone.sync.call(this, method, model, options); 
    } 
}); 

然后在你的模型:

var MyModel = BaseModel.extend({ 
    // model stuff 
}); 
+0

方式比我更好的解决方案 – 2012-01-02 19:45:24

1

想着它,我想出了这个解决方案:

function callback(success){ 
    this.success = sucess; 
} 

callback.prototyp.error = function(model, response){ 
    // central error handling here 
} 

myModel.save(new callback(myModel.success)) 
相关问题