Backbone Model mixin that provides toJSON(), parse(), and validate() implementations for models with nested types.
Setup:
-
Mix this object into a Backbone Model using
Backbone.Model.extend(NestedTypesMixin). -
Define nested-type attributes in one of the following ways:
A. Set
nestedTypesin the Model definition.
B. SetnestedTypeson the model instance.Note:
nestedTypescan either be an object or a function.
Sample Setup and Usage:
var MyModel = Backbone.Model.extend({
nestedTypes: {
subModel: Backbone.Model
}
}).extend(NestedTypesMixin);
// `subModel` is converted to an instance of `Backbone.Model` because `{ parse: true }` is passed.
var model = new MyModel({ subModel: { foo: 'bar' } }, { parse: true });
model.get('subModel');// => returns the Backbone Model
// `subModel` is converted to a plain object using it's own `toJSON()` method
model.toJSON();// => { subModel: { foo: 'bar' } }