Backbone.ComputedFields
Inspired by Derik Bailey's Backbone.Computed, Backbone.ComputedFields aims the same goal, but polished for real project needs.
Quick start
Instantiated in initialize
method,
{ thiscomputedFields = this;}
ComputedField is declared as computed
in model,
computed:
All properties inside are treated as computed fields.
computed: grossPrice: { return 105; }
computed
can also be a function returning an object.
{ return grossPrice: { return 105; } ;}
Each property that declares get
or set
method is treated as computed.
Get the value of computed property,
model; // -> 105 is returned
Dependent fields
In case that computed field depends on some other models fields,
computed: grossPrice: depends: 'netPrice' 'vatRate' { return fieldsnetPrice * 1 + fieldsvatRate / 100; }
Add depends
object into computed field object, as array of dependent fields. Dependent fields are injected into corresponding get
method, by passing initialized fields
object inside,
var Model = BackboneModel; model = netPrice: 100 vatRate: 20;model // -> 120 is returned
Setting computed values
Besides of get
computed field might have set
method as well.
computed: grossPrice: depends: 'netPrice' 'vatRate' { return fieldsnetPrice * 1 + fieldsvatRate / 100; } { fieldsnetPrice = value / 1 + fieldsvatRate / 100; }
set
function receives the fields
object, with same names of properties as model attributes. If set
function changes the value of property, the change is propagated to model. Typically, you should change only one field in set
method.
Model changes
In case of depended field is changed, computed field is automatically updated
model;model; // -> 105 is returned // or model;model; // -> 126 is returned
In case of calculated field is changed, dependent field in automatically updated
model;model; // -> 100 is returned
Model events
To make views works correctly, it important to keep correct events distribution.
In case of depended field is changed,
model;
After that call, several events are triggered - change:netPrice
, as a reaction of grossPrice
updated, change:grossPrice
is triggered.
In case of computed field is changed,
model;
After that call, several events are triggered - change:grossPrice
, as a reaction of netPrice
updated, change:netPrice
is triggered.
Model validation
The same rules as for usual Backbone.js model attributes rules are applied for computed ones. If model contains validate()
method and invalid is being set, the change would not propagate into model attributes, error
event is triggered instead.
Say, we have such validation function,
{ var errors = ; if !_ || attrsnetPrice < 0 errors; if !_ || attrsgrossPrice < 0 errors; return errorslength > 0 ? errors : false;}
And change computed field,
model;
The model is will remain in valid state, { netPrice: 100, vatRate: 20, grossPrice: 120 }
.
Dependency function
Computed field might have dependency not only on internal model attributes, but on external objects too. For instance, the product show price depends on currency selected by user in currency widget. Besides properties names, depends: []
can accept function, that is responsible to fire callback if change occurred.
computed: grossPrice: depends: 'netPrice' 'vatRate' { thisexternal; } { return thisexternal; }
JSON payload
By default all computed fields are treated as part of JSON payload,
model // -> { "netPrice": 100, "grossPrice": 120, "vatRate": 20 };
To disable that add toJSON: false
in computed field declaration,
computed: grossPrice: depends: 'netPrice' 'vatRate' { return fieldsnetPrice * 1 + fieldsvatRate / 100; } { fieldsnetPrice = value / 1 + fieldsvatRate / 100; } toJSON: false
If you'd like to force the computed fields into the JSON payload even if the toJSON
option is false
, pass
computedFields: true
to the toJSON
function:
model
More details
Up-to-date and complete documentation is located at /test/spec/backbone.computedfields.spec.js.
Legal Info (MIT License)
Copyright (c) 2012 Alexander Beletsky
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.