Skip to content

Instantly share code, notes, and snippets.

@elliotf
Last active December 15, 2015 14:49
Show Gist options
  • Save elliotf/5277447 to your computer and use it in GitHub Desktop.
Save elliotf/5277447 to your computer and use it in GitHub Desktop.
What I ended up using after smacking my face against backbone relational and backbone associations
//= require ../collections/children.js
(function($){
demo.models.Parent = Backbone.Model.extend({
initialize: function(options) {
this.children = new demo.collections.Children();
this.attributes = this.parse(this.attributes);
}
, exampleJSON: {
someAttr: 'some value'
, children: [
{ content: "valid contents for other model"}
, { content: "more valid contents for other model"}
]
}
// override parse and toJSON to handle children as a collection
, parse: function(json){
if (this.children) {
json.modified = false;
this.children.reset(json.children);
delete json.children;
}
return json;
}
, toJSON: function(){
var json = Backbone.Model.prototype.toJSON.apply(this);
json.children = this.children.toJSON();
return json;
}
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment