Javascript 在初始化视图之前使用主干提取成功回调更改数据
我正在寻找一种方法,当我从服务器获取主干模型(严格地说是集合)时,截取从服务器返回的值,然后在继续之前对其进行修改。我想我可以做这样的事情Javascript 在初始化视图之前使用主干提取成功回调更改数据,javascript,backbone.js,Javascript,Backbone.js,我正在寻找一种方法,当我从服务器获取主干模型(严格地说是集合)时,截取从服务器返回的值,然后在继续之前对其进行修改。我想我可以做这样的事情 SessionController.prototype._initPages = function() { return App.pages.fetch({ reset: true, success: function(model, response, options) { //modify the cont
SessionController.prototype._initPages = function() {
return App.pages.fetch({
reset: true,
success: function(model, response, options) {
//modify the contents of response
}
};
我的修改将反映在用于初始化视图的模型中
然而,我正在查看主干来源,我想我可能误解了一些东西
fetch: function(options) {
options = options ? _.clone(options) : {};
if (options.parse === void 0) options.parse = true;
var success = options.success;
var collection = this;
options.success = function(resp) {
var method = options.reset ? 'reset' : 'set';
collection[method](resp, options); //this line updates the model
if (success) success(collection, resp, options); // my success callback
collection.trigger('sync', collection, resp, options);
};
wrapError(this, options);
return this.sync('read', this, options);
}
出于我的需要,这两个注释行似乎需要切换,尽管我认为我只是误解了如何使用此功能
如何在服务器响应成为我的模型之前修改它?我认为您可以根据需要重写
parse
函数来修改数据