This plugin allow you define schemas for your models. It provides type control, computable properties, references between models and collections, filters and localization.
Dependencies:
- Backbone
>= 1.1.0
- Underscore
>= 1.5.2
- Globalize
>= 0.1.1
First you need to define model and schema.
var model = new Backbone.Model(), schema = new Backbone.Schema(model);
Now you got instances of model and schema and you can start defining properties of the model. Use schema.define(attribute, options)
method to do it.
Converts value to string. Represents as is.
schema.define('string-property', { type: 'string' });
model.set('string-property', 999999.99); // --> "999999.99"
model.get('string-property'); // <-- "999999.99"
Converts value to boolean. Represents as is.
schema.define('boolean-property', { type: 'boolean' });
model.set('boolean-property', 'true'); // --> true
model.get('boolean-property'); // <-- true
Converts value to number. Represents as is, or as formatted string (depends from option format
).
Additional options:
format
- see more about number formattingculture
- see more about cultures
schema.define('number-property', { type: 'number', format: 'n', culture: 'en' });
model.set('number-property', '999,999.99'); // --> 999999.99
model.get('number-property'); // <-- "999,999.99"
Converts value to Date, ISO 8601, or Unix time. Represents as is, or as formatted string (depends from option format
).
Additional options:
format
- see more about date formattingstandard
(available values areiso
andunix
) - defines in which way date will be stored in the model (if not specified, date will be stored as Date)culture
- see more about cultures
schema.define('datetime-property', { type: 'datetime', format: 'd', standard: 'unix', culture: 'en' });
model.set('datetime-property', '12/12/2012'); // --> 1355263200000
model.get('datetime-property'); // <-- "12/12/2012"
Converts value to key of localization. Represents as is, or as string of localization (depends from availability of localization).
Additional options:
culture
- see more about cultures and localization
Globalize.addCultureInfo('en', {
messages: {
'HELLO_WORLD': 'Hello, World!'
}
});
schema.define('locale-property', { type: 'locale', culture: 'en' });
model.set('locale-property', 'Hello, World!'); // --> "HELLO_WORLD"
model.get('locale-property'); // <-- "Hello, World!"
Besides listed above data types you can define arrays. To do this just use option array
instead of type
. For example: { array: 'string' }
, { array: 'number' }
etc. In this case each item in array will be processed by corresponding handler.
You can define a computed properties with your own custom logic.
Manipulate these two options to describe behavior of a computed property.
var User = Backbone.Model.extend({
initialize: function () {
var schema = new Backbone.Schema(this);
schema.define('fullName', {
getter: function (attribute, value) {
var firstName = this.model.get('firstName'),
lastName = this.model.get('lastName');
return firstName + ' ' + lastName;
},
setter: function (attribute, value) {
var fullName = value.match(/\S+/g);
return {
firstName: fullName[0],
lastName: fullName[1]
};
}
});
}
});
var user = new User({
firstName: 'Dmytro',
lastName: 'Nemoga'
});
user.get('fullName'); // <-- "Dmytro Nemoga"
user.set('fullName', 'Andriy Serputko'); // --> firstName: "Andriy", lastName: "Serputko"
Converts value to model, using value as a set of attributes. Represents as is.
Additional options:
clear
- iftrue
, model removes an existing attributes before setting new (otherwise merges them)
schema.define('nested-model', { model: Backbone.Model });
model.set('nested-model', { id: 0, value: 'foo' }); // --> new Backbone.Model({ id: 0, value: 'foo' })
model.get('nested-model'); // <-- instance of Backbone.Model
Converts value to collection, using value as a set of attributes. Represents as is.
Additional options:
reset
- iftrue
, collection removes an existing models before creating new (otherwise merges them)
schema.define('nested-collection', { collection: Backbone.Collection });
model.set('nested-collection', [ // --> new Backbone.Collection([
{ id: 1, value: 'bar' }, // { id: 1, value: 'bar' },
{ id: 2, value: 'baz' }, // { id: 2, value: 'baz' },
{ id: 3, value: 'qux' } // { id: 3, value: 'qux' }
]); // ])
model.get('nested-collection'); // <-- instance of Backbone.Collection
Before using reference models or collections make sure that you have a source collection.
var sourceCollection = new Backbone.Collection([
{ id: 0, value: 'foo' },
{ id: 1, value: 'bar' },
{ id: 2, value: 'baz' },
{ id: 3, value: 'qux' }
]);
If you pass collection in this option, plugin tries to get model from it.
Converts value (a model ID in the source collection) to model, keeping reference to original model. Represents as is.
schema.define('reference-model', { type: 'model', source: sourceCollection });
model.set('reference-model', 0); // --> sourceCollection.get(0)
model.get('reference-model'); // <-- instance of Backbone.Model
Converts value (a set of model IDs in the source collection) to collection, keeping references to original models. Represents as is.
schema.define('reference-collection', { type: 'collection', source: sourceCollection });
model.set('reference-collection', [ // --> new Backbone.Collection([
1, // sourceCollection.get(1),
2, // sourceCollection.get(2),
3 // sourceCollection.get(3)
]); // ])
model.get('reference-collection'); // <-- instance of Backbone.Collection
The plugin prevents setting undefined
values, instead of this it assigns a default value or null
for regular properties, {}
for models and []
for collections and arrays.
- Fixed issue with nested models and collections
Backbone.Schema
could be extended
- Added CommonJS support
- Removed
text
type number
anddatetime
requiresformat
option for string output
- Fixed behavior for
model
andcollection
types
- Renaming option
reset
toclear
formodel
type - Changed default behavior for
model
andcollection
types
- Renaming
types
tohandlers
- Method
refresh
moved from model to schema - Removed backward reference to schema
refresh
affects only registered attributesmodel
andcollection
attributes cannot benull
- Option
arrayOf
renamed toarray
- Option
fromSource
renamed tosource
- Added option to use a custom culture
- Fixed serious issue with
model
type
- Handlers
currency
andpercent
merged intonumber
- Plugin implemented as decorator, not a class
- Option
reset
formodel
andcollection
types
- Properties are configurable with additional options
- Formatters and converters merged into types
- Added support of reference models and collections
- A lot of fixes
- Added support of nested models and collections
- Support of arrays
- Added type
locale
- Removed method
toJSON
- Formatters and converters takes only
value
argument
- Static methods runs in correct context, now they may be used as independent helpers
- Properties
formatters
andconverters
is static
- Removed CommonJS support
- Added CommonJS support
- Integration with project Backbone.Accessors
- Method
defineProperty
renamed toproperty
- Methods
addGetter
/addSetter
merged to methodcomputed
- Option
advanced
oftoJSON
method renamed toschema
- Removed argument
options
ofdefineProperty
method's
- Method
addProperty
renamed todefineProperty
- Initial release