promised-models
Advanced tools
Comparing version 0.0.6 to 0.0.8
@@ -47,2 +47,8 @@ /** | ||
/** | ||
* when false calculation errors will be silent | ||
* @type {Boolean} | ||
*/ | ||
throwCalculationErrors: true, | ||
/** | ||
* if model was synced with storage | ||
@@ -256,3 +262,3 @@ * @return {Boolean} | ||
* @param {Function} cb | ||
* @param {*} [ctx] [description] | ||
* @param {*} [ctx] | ||
*/ | ||
@@ -285,3 +291,3 @@ on: function (attribute, event, cb, ctx) { | ||
return model._readyPromise.always(function (p) { | ||
//promise not changed | ||
//promise not changed or rejected | ||
if (model._readyPromise === p) { | ||
@@ -304,6 +310,23 @@ return p; | ||
} | ||
return this._readyPromise; | ||
if (this.throwCalculationErrors) { | ||
return this._readyPromise; | ||
} else { | ||
return this._readyPromise.always(function () { | ||
return Vow.fulfill(); | ||
}); | ||
} | ||
}, | ||
_calculate: function () { | ||
/** | ||
* to prevent loop calculations we limit it | ||
* @type {Number} | ||
*/ | ||
maxCalculations: 100, | ||
/** | ||
* @param {Number} [n = 0] itteration | ||
* @return {Promise} | ||
*/ | ||
_calculate: function (n) { | ||
var model = this, | ||
@@ -313,3 +336,11 @@ calculations = {}, | ||
nestedCalculations = []; | ||
n = n || 0; | ||
if (n >= model.maxCalculations) { | ||
return model._throwCalculationLoop(); | ||
} | ||
model.commit(model.CALCULATIONS_BRANCH); | ||
model._attributesAr.forEach(function (attribute) { | ||
@@ -333,3 +364,3 @@ if (attribute.calculate) { | ||
if (model.isChanged(model.CALCULATIONS_BRANCH)) { | ||
return model._calculate(); | ||
return model._calculate(++n); | ||
} else { | ||
@@ -341,2 +372,21 @@ model._triggerEvents(); | ||
/** | ||
* @return {Promise<, {Error}>} rejected promise | ||
*/ | ||
_throwCalculationLoop: function () { | ||
var model = this, | ||
changedFields = model._attributesAr.filter(function (attribute) { | ||
return attribute.isChanged(model.CALCULATIONS_BRANCH); | ||
}).map(function (attribute) { | ||
return attribute.name; | ||
}); | ||
return Vow.reject(new Error( | ||
'After ' + | ||
model.maxCalculations + | ||
' calculations fileds ' + | ||
changedFields + | ||
' still changed' | ||
)); | ||
}, | ||
_triggerEvents: function () { | ||
@@ -343,0 +393,0 @@ var model = this, |
{ | ||
"description": "promise based, typed attributes, nested models and collections", | ||
"name": "promised-models", | ||
"version": "0.0.6", | ||
"version": "0.0.8", | ||
"repository": "git@github.com:delfrrr/promised-models.git", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -1,2 +0,2 @@ | ||
# Promised Models (in progress) | ||
# Promised Models | ||
@@ -37,3 +37,3 @@ ## Key features | ||
Creates you own model class by extending `Model`. You can define attributes, instance/class method and properties. Inheritance is build over [inherit](https://www.npmjs.com/package/inherit). | ||
Creates you own model class by extending `Model`. You can define attributes, instance/class method and properties. Inheritance is built over [inherit](https://www.npmjs.com/package/inherit). | ||
@@ -115,3 +115,3 @@ ```js | ||
model.attributes.name.get(); //Kate | ||
model.get('some'); //throws error as uknown attribute | ||
model.get('some'); //throws error as unknown attribute | ||
``` | ||
@@ -123,3 +123,3 @@ | ||
**Note:** You can create internal attributes, wich wouldn't be included to returned object. | ||
**Note:** You can create internal attributes, which wouldn't be included to returned object. | ||
@@ -232,6 +232,6 @@ ```js | ||
```js | ||
//sunscribe | ||
//subscribe | ||
model.on('change:weight change:name', this.changeHandler, this); | ||
//unsunscribe | ||
//unsubscribe | ||
model.un('change:weight change:name', this.changeHandler, this); | ||
@@ -279,3 +279,3 @@ ``` | ||
Fullfils when all calculations over model finished. | ||
Fulfils when all calculations over model finished. | ||
@@ -307,3 +307,3 @@ ```js | ||
Fetch data associlated with model from storage. | ||
Fetch data associated with model from storage. | ||
@@ -375,3 +375,3 @@ ```js | ||
This methods provided for advanced model extending. Consult source for detials. | ||
These methods provided for advanced model extending. Consult source for details. | ||
@@ -405,2 +405,48 @@ ### Model static methods and properties | ||
### List | ||
Array like object returned for fields types `List` and `ModelsList` | ||
``` | ||
var Podium = Model.inherit({ | ||
attributes: { | ||
models: Model.attributeTypes.ModelsList(FashionModel) | ||
} | ||
}), | ||
podium = new Podium(data), | ||
list = podium.get('models'), //instanceof List | ||
model = list.get(0); //instanceof Model | ||
``` | ||
#### Mutating methods | ||
List inerits Array mutating methods: `pop`, `push`, `reverse`, `shift`, `sort`, `splice`, `unshift` | ||
``` | ||
podium.get('models').push(new FashionModel()); | ||
``` | ||
#### `list.get(index)` | ||
Get list item by index | ||
``` | ||
podium.get('models').get(0);// instanceof Model | ||
``` | ||
#### `list.length()` | ||
Returns length of list | ||
#### `list.toArray()` | ||
Returns shallow copy of Array, wich stores List items | ||
``` | ||
podium.get('models').forEach(function (model) { | ||
model; // instanceof Model | ||
}); | ||
``` | ||
#### ValidationError `Model.ValidationError` | ||
@@ -407,0 +453,0 @@ |
var expect = require('chai').expect; | ||
var expect = require('chai').expect, | ||
Vow = require('vow'), | ||
Model = require('../lib/model'); | ||
@@ -75,2 +77,31 @@ describe('Calculate', function () { | ||
it('should stop after iteration limit', function () { | ||
var LoopedModel = Model.inherit({ | ||
throwCalculationErrors: false, | ||
attributes: { | ||
a: Model.attributeTypes.String.inherit({ | ||
default: 'a-0', | ||
isChanged: function () { | ||
return this.get() !== this.default; | ||
}, | ||
}), | ||
b: Model.attributeTypes.String.inherit({ | ||
calculate: function () { | ||
return this.model.get('a') + '-1'; | ||
} | ||
}) | ||
} | ||
}), | ||
loopedModel = new LoopedModel(); | ||
return loopedModel.ready().then(function () { | ||
loopedModel.set('a', 'a-1'); | ||
return loopedModel.ready(); | ||
}).always(function (p) { | ||
var err = p.valueOf(); | ||
expect(err).instanceof(Error); | ||
expect(err.message).to.contain('After 100 calculations'); | ||
return Vow.fulfill(); | ||
}); | ||
}); | ||
}); | ||
@@ -77,0 +108,0 @@ describe('Amend', function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86879
2192
451