Created
June 5, 2011 06:10
-
-
Save jachenry/1008702 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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