promised-models
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -112,3 +112,3 @@ /** | ||
var model = this; | ||
return fulfill().then(function () { | ||
return this.ready().then(function () { | ||
return model.storage.find(model); | ||
@@ -332,5 +332,9 @@ }).then(function (data) { | ||
calculate: function () { | ||
var model = this; | ||
if (this.isReady()) { | ||
this.trigger('calculate'); | ||
this._readyPromise = this._calculate(); | ||
//start _calculate on next tick | ||
this._readyPromise = fulfill().then(function () { | ||
return model._calculate(); | ||
}); | ||
} else { | ||
@@ -377,2 +381,4 @@ this._requireMoreCalculations = true; | ||
this._requireMoreCalculations = false; | ||
model.commit(model.CALCULATIONS_BRANCH); | ||
@@ -396,3 +402,4 @@ | ||
]).spread(function (calculateData) { | ||
model.set(calculateData); | ||
model._setCalculatedData(calculateData); | ||
// model.set(calculateData); | ||
if (model._checkContinueCalculations()) { | ||
@@ -410,6 +417,15 @@ return model._calculate(++n); | ||
/** | ||
* setting calculated data only if nothing have changed during calculations | ||
* otherwise we will have racing conditions( | ||
* @param {object} calculateData | ||
*/ | ||
_setCalculatedData: function (calculateData) { | ||
if (!this._checkContinueCalculations()) { | ||
this.set(calculateData); | ||
} | ||
}, | ||
_checkContinueCalculations: function () { | ||
var toContinue = this.isChanged(this.CALCULATIONS_BRANCH) || this._requireMoreCalculations; | ||
this._requireMoreCalculations = false; | ||
return toContinue; | ||
return this.isChanged(this.CALCULATIONS_BRANCH) || this._requireMoreCalculations; | ||
}, | ||
@@ -416,0 +432,0 @@ |
{ | ||
"description": "promise based, typed attributes, nested models and collections", | ||
"name": "promised-models", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"repository": "git@github.com:delfrrr/promised-models.git", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -30,2 +30,45 @@ | ||
describe('fetch', function () { | ||
var ModelClass; | ||
before(function () { | ||
ModelClass = Model.inherit({ | ||
isNew: function () { | ||
return false; | ||
}, | ||
attributes: { | ||
a: Model.attributeTypes.String.inherit({ | ||
default: 'a' | ||
}), | ||
b: Model.attributeTypes.String.inherit({ | ||
calculate: function () { | ||
if (!this.get()) { | ||
return this.model.get('a') + 'b'; | ||
} else { | ||
return this.get(); | ||
} | ||
} | ||
}) | ||
}, | ||
storage: Model.Storage.inherit({ | ||
find: function (model) { | ||
return { | ||
a: model.get('a') + '-fetched', | ||
b: model.get('b') + '-fetched' | ||
}; | ||
} | ||
}) | ||
}); | ||
}); | ||
it('should run after calculations', function () { | ||
var model = new ModelClass(); | ||
return model.fetch().then(function () { | ||
expect(model.toJSON()).to.be.deep.equal({ | ||
a: 'a-fetched', | ||
b: 'ab-fetched' | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('isNew', function () { | ||
@@ -32,0 +75,0 @@ it('should be true after model create', 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
106188
2733