2013-10-17 23 views
0

我使用Backbone.Paginator clientPager。Backbone.paginator - 獲取toJSON的origModels

目前它複製從this.origModels中的服務器獲取的模型。並修改this.models截斷分頁版本。

當我做collection.toJSON我只得到截斷版本。我想獲得origModels的toJSON?我試圖調查Backbone代碼的內部。但不能成功。

從Backbone.js的

toJSON: function(options) { 
    return this.map(function(model){ return model.toJSON(options); }); 
} 

var methods = ['forEach', 'each', 'map', 'collect', 'reduce', 'foldl', 
    'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select', 
    'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke', 
    'max', 'min', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest', 
    'tail', 'drop', 'last', 'without', 'indexOf', 'shuffle', 'lastIndexOf', 
    'isEmpty', 'chain']; 

// Mix in each Underscore method as a proxy to `Collection#models`. 
_.each(methods, function(method) { 
    Collection.prototype[method] = function() { 
    var args = slice.call(arguments); 
    args.unshift(this.models); 
    return _[method].apply(_, args); 
    }; 
}); 

我試着做以下,但沒有工作:(

var args = [].slice.call(function(model) { return model.toJSON(); }); 
args.unshift(this.origModels); 
var jsonmodels = _['map'].apply(_, args); 

回答

0

我用下面的代碼解決了這個,

var jsonmodels = _.map(this.collection.origModels, function(model){ return model.toJSON(); });