Reputation: 143
How can I use a function in view when it is in collection ( how can I refer to it from another file) e.g. I have this pagination function in collection
pagination : function(perPage, page) {
page = page - 1;
var collection = this;
collection = _(collection.rest(perPage * page));
collection = _(collection.first(perPage));
return collection.map( function(model) {
return model.toJSON();
});
},
Thats what I have tried in view but not correct:
var paginatedCollection = new BansCollection(pagination(this.pageSize,pageToLoad));
Upvotes: 0
Views: 27
Reputation: 3789
When you initialize
your view you can also pass your collection with other options.
var myView = new MyView({collection: myCollection});
Then in your view you can reference to it using this.collection
As @JonasGeiregat suggests, view might not be a suitable place for pagination logic. So after you move your pagination logic to the collection you can do this within your view.
var models = this.collection.paginate(pageSize, page);
// render do whatever to print out those models/json
Upvotes: 0
Reputation: 5442
It feels to me that a function that acts upon a collection should not be placed within a Backbone View object. I would put the pagination function in your BansCollection so you can do something like this in your view code:
var paginatedCollection = new BansCollection();
paginatedCollection.pagination(this.pageSize, pageToLoad);
Upvotes: 1