Skip to content

Instantly share code, notes, and snippets.

@jachenry
Created June 5, 2011 06:10
Show Gist options
  • Save jachenry/1008702 to your computer and use it in GitHub Desktop.
Save jachenry/1008702 to your computer and use it in GitHub Desktop.
(function(Spine, $){
var createCollection = function(model, record){
var collection = model.setup(model.name, model.attributes);
collection.extend(Spine.Model.Ajax);
collection.extend({
url: function(){
return record.url() + "/" + this.name.toLowerCase() + "s"
}
});
for(var key in model.many){
collection.hasMany(key, model.many[key]);
}
return collection;
}
Spine.Model.extend({
hasMany: function(name, model){
this.attributes.push(name);
this.many = this.many || {};
this.many[name] = model
this.bind("init", function(record){
var attribute = record[name];
record[name] = createCollection(model, record);
if(attribute){
record[name].refresh(attribute.records || attribute)
}
})
}
})
})(Spine, Spine.$);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment