Backbone.paginator - Get toJSON of origModels

161 Views Asked by At

I'm using Backbone.Paginator clientPager.

Currently it copies the models fetched from the server inside this.origModels. And modifies this.models to the truncated paginated version.

When I do collection.toJSON I get only the truncated version. I would like to get the toJSON of origModels? I tried investigating the internals of Backbone code. But couldn't succeed.

From 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);
  };
});

I tried to do the following but didn't work :(

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

There are 1 best solutions below

0
On

I fixed this by using the following code,

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