The plugin is for defining model's properties with type specifying.
Dependencies:
- Backbone
>= 1.0.0
- Underscore
>= 1.4.4
- Globalize
>= 0.1.1
- Object
formatters
- Function
string
- Function
number
- Function
boolean
- Function
date
- Function
text
- Function
currency
- Function
percent
- Function
- Object
converters
- Function
string
- Function
number
- Function
boolean
- Function
date
- Function
text
- Function
currency
- Function
percent
- Function
- Function
property(attribute, type)
- String
attribute
- String
type
- String
- Function
computed(attribute, options)
- String
attribute
- Object
options
- Function
getter
- Function
setter
- Function
- String
- Function
toJSON([options])
- Object
options
- Boolean
schema
- Boolean
- Object
var model = new Backbone.Model();
Converts value to the string. Represents as is.
model.property('stringProperty', 'string');
model.set('stringProperty', 999999.99); // model.attributes.stringProperty -> "999999.99"
model.get('stringProperty'); // "999999.99"
Converts value to the number. Represents as the string in a format of current culture.
model.property('numberProperty', 'number');
model.set('numberProperty', '999,999.99'); // model.attributes.numberProperty -> 999999.99
model.get('numberProperty'); // "999,999.99"
Converts value to the boolean. Represents as is.
model.property('booleanProperty', 'boolean');
model.set('booleanProperty', 'true'); // model.attributes.booleanProperty -> true
model.get('booleanProperty'); // true
Converts value to the Unix time. Represents as the string in a format of current culture.
model.property('dateProperty', 'date');
model.set('dateProperty', '12/12/2012'); // model.attributes.dateProperty -> 1355263200000
model.get('dateProperty'); // "12/12/2012"
Converts value to the string, escaping an unsafe characters. Represents an unescaped string.
model.property('textProperty', 'text');
model.set('textProperty', '<b>text</b>'); // model.attributes.textProperty -> "<b>text</b>"
model.get('textProperty'); // "<b>text</b>"
Converts value to the number. Represents as the string in a format of current culture.
model.property('currencyProperty', 'currency');
model.set('currencyProperty', '$999,999.99'); // model.attributes.currencyProperty -> 999999.99
model.get('currencyProperty'); // "$999,999.99"
Converts value to the hundredths of number. Represents as the string in a format of current culture.
model.property('percentProperty', 'percent');
model.set('percentProperty', '99.99 %'); // model.attributes.percentProperty -> 0.9999
model.get('percentProperty'); // "99.99 %"
// Define formatter
Backbone.Model.formatters.hex = function (attribute, value) {
return '0x' + value.toString(16).toUpperCase();
};
// Define converter
Backbone.Model.converters.hex = function (attribute, value) {
return parseInt(value, 16);
};
model.property('hexProperty', 'hex');
model.set('hexProperty', '0xFF'); // model.attributes.hexProperty -> 255
model.get('hexProperty'); // "0xFF"
Without options toJSON
works as original method.
model.toJSON();
{
"stringProperty": "string",
"numberProperty": 999999.99,
"booleanProperty": true,
"dateProperty": 1355263200000,
"textProperty": "<b>text</b>",
"currencyProperty": 999999.99,
"percentProperty": 0.9999,
"hexProperty": 255
}
With { schema: true }
option method toJSON
will return a formatted representation.
model.toJSON({ schema: true });
{
"stringProperty": "string",
"numberProperty": "999,999.99",
"booleanProperty": true,
"dateProperty": "12/12/2012",
"textProperty": "<b>text</b>",
"currencyProperty": "$999,999.99",
"percentProperty": "99.99 %",
"hexProperty": "0xFF"
}
// Create model
var user = new Backbone.Model({
firstName: 'Dmytro',
lastName: 'Nemoga'
});
// Define computed property
user.computed('fullName', {
getter: function (attribute, value) {
var firstName = this.get('firstName'),
lastName = this.get('lastName');
return firstName + ' ' + lastName;
},
setter: function (attribute, value) {
var fullName = value.split(' ');
return {
firstName: fullName[0],
lastName: fullName[1]
};
}
});
// Get computed property
user.get('fullName'); // "Dmytro Nemoga"
// Set computed property
user.set('fullName', 'Andriy Serputko'); // user.attributes -> { firstName: "Andriy", lastName: "Serputko" }
The plugin prevents setting undefined
values, instead of this it assigns a default value or null
.
- 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