Backbone Model Decorator
Backbone Model Decorator is a Backbone.js plugin that decorates Backbone.Model
with properties
property that plays role of Model Facade and contains model attributes in form of properties.
var DecoratedModel = ModelDecorator;var decorated = ;
Getting model attribute via such property will return current value of the attribute.
console; // default@email.comdecorated; console; // @!$^&SEvjH#
And setting new value to property will immediately change model attribute.
decoratedpropertiesemail = 'other@email.com';console; // other@email.comdecoratedpropertiesemail = '@!$^&SEvjH#';console; // @!$^&SEvjH#
Installation
This package is available via npm
npm install backbone-model-decorator --save
and bower distribution systems
bower install backbone-model-decorator --save
Also you can download it as single file with comments or minified from this repository.
Usage
To start using decorator you should extend model from ModelDecorator
instead of Backbone.Model.
var decorated = ModelDecorator;
With extending from ModelDecorator
, extended model gains new properties and methods.
properties
-- Facade object with all generated propertiespropertyOptions
-- Object containing options for model attributes that should be used by default when setting new value via setter functionvalidateProperties()
-- Method that allows creating bunch of properties on Facade object by going through passed object's property namesproperty()
-- Method that allows adding or changing single property on Facade object
Worth noting that Model should know
about possible attributes it might have to properly initialize Facade properties.
Facade object is populated with properties when model initialization occurs, so it has 3 sources for possible properties:
- Model
attributes
object - Model
defaults
object - Model
propertyOptions
object that contains default options applied on property set These objects are checked for property names and each found property will be mirrored on facade object as possible Model attribute.
There are two ways to add properties to Facade after model initialization
- Using
Model.validateProperties()
- Using
Model.property()
Also its possible to re-define property viaModel.property()
adding custom getter/setter functions or making read-only properties.
Model.properties
Default place where Model Facade object will be stored is properties
field in Model object. This can be changed in 2 ways.
Set ModelDecorator
to create properties using Model object without creating Facade object. All newly created properties will be available directly from model.
ModelDecoratorfacadeType = ModelDecoratorUSE_MODEL;var DecoratedModel = ModelDecorator; var decorated = ; console; // nullconsole; // this is valueconsole; // this is name decoratedvalue = 'new value';decorated; console; // new valueconsole; // update name
Note: Creating properties on model may cause name collisions with internal Backbone Model instance fields.
Or change Facade property name, by default its properties
. In this case Facade will be created and stored in field with specified name. All created properties will be stored in Facade object.
ModelDecoratorfacadeFieldName = 'props'; var DecoratedModel = ModelDecorator;var decorated = ; console; // nullconsole; // ModelFacade {} console; // this is valueconsole; // this is name decoratedpropsvalue = 'new value';decorated; console; // new valueconsole; // update name
Note: Renaming facade property to names used by Backbone Model, like attributes
, may cause unexpected errors.
Model.validateProperties()
validateProperties()
does lookup for names on passed object and uses this names to create new properties on facade.
It will not apply values from passed objects, so new model properties will be created but not initialized.
var DecoratedModel = ModelDecorator;var decorated = ;decorated; console; // trueconsole; // undefinedconsole; // undefined decoratedpropertiesvalue = 'value1';decoratedpropertiesname = 'new name'; console; // Object {value: "value1", name: "new name"}console; // value1console; // new name
Model.property()
This method has up to 4 arguments with only first argument required, other are optional.
name:String
-- Model attribute name for which property should be createdoptions:Object
-- Object containing options that should be used on every value updatesetter:Function|Boolean
-- Accepts custom setter function ortrue
if property should be read-onlygetter:Function
-- Custom getter function
With this method its easy to create computed properties that will not mess with original model
var DecoratedModel = ModelDecorator;var decorated = ;decorated;console; // emailTo:default@email.com
It always returns property options, if called with name of existing property it will do nothing but return this property options.
var DecoratedModel = ModelDecorator;var decorated = ; console; // trueconsole; // 2
ModelDecorator.createPropertyGetter/Setter()
ModelDecorator has 2 statics methods that called when getter or setter should be generated. These methods can be replaced by custom implementation.
ModelDecorator.createPropertyGetter(model, name)
-- property getter method generator. Generates function that will call model's.get()
method withname
and return result of this call.ModelDecorator.createPropertySetter(model, name)
-- property setter method generator. Generates function that will create object with property for model update and call model's.set()
method passing created object and property options to it.
They have identical signature, as parameters model and property name should be passed and both will return function as result.
ModelDecorator.createPropertyGettermodel:Backbone.Model, name:String:Function