Last active
December 15, 2015 14:49
-
-
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
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
//= 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