bitovi/canjs — pull request #427 by retro (2013-06-12 11:13:49+00:00)
Title: Create model collection plugin

This PR implements the `can.Model.Collection` plugin.

`can.Model.Collection` combines params and list of items under one class which does two things:

1. It reduces the amount of code needed to wire up filterable and sortable `findAll` requests
2. It removes code related to the data loading from the controllers

Without the `can.Model.Collection` plugin, code would look like this:

var ImagesControl = can.Control({
init : function(){
this.options.params = new can.Observe;
this.on();
this.imagesList = new can.Observe.List

this.element.html(someView({
imagesList : this.imagesList
}))

this.loadData()
},
'a click' : function(el, ev){
this.options.params.attr('foo', 'bar');
},
"{params} change" : 'loadData',
loadData : function(){
ImageModel.findAll(this.options.params.serialize(), this.proxy('updateData'));
},
updateData : function(data){
this.imagesList.replace(data);
}
})

`can.Model.Collection` shortens it to this:

var ImagesControl = can.Control({
init : function(){
this.imageCollection = new ImageModel.Colle;tion:

this.element.html(someView({
imagesList : this.imageCollection.list
}))

this.imageCollection.load();
},
'a click' : function(el, ev){
this.imageCollection.params.attr('foo', 'bar');
}
})