JSFiddle - React, Tailwind, and code Playground
by paulL
HTML
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
JavaScript
Backbone.Model.prototype.toJSON = function() {
var json = _.clone(this.attributes);
for(var attr in json) {
if((json[attr] instanceof Backbone.Model) || (json[attr] instanceof Backbone.Collection)) {
json[attr] = json[attr].toJSON();
}
}
return json;
};
var Child = Backbone.Model.extend({
defaults: {
child: 1
}
});
var Children = Backbone.Collection.extend({
model: Child
});
var AnotherChild = Backbone.Model.extend({
defaults: {
anotherChild: 1
}
});
var Parent = Backbone.Model.extend({
defaults: {
parent: 1
},
initialize: function() {
this.set('child', new Children([new Child, new Child]));
this.set('anotherChild', new AnotherChild);
}
});
console.log((new Parent).toJSON());