promised-models
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -31,3 +31,3 @@ /** | ||
this.storage = new Storage(); | ||
this.attributes = Object.keys(this.attributes || {}).reduce(function (attributes, name) { | ||
this.attributes = Object.keys(this.__self.attributes || {}).reduce(function (attributes, name) { | ||
var Attribute = model.attributes[name]; | ||
@@ -61,3 +61,2 @@ attributes[name] = new (Attribute.inherit({ | ||
/** | ||
@@ -69,19 +68,20 @@ * save model changes | ||
var model = this; | ||
if (model.isNew()) { | ||
return model.ready().then(function () { | ||
return model.storage.insert(model); | ||
}).then(function (id) { | ||
model.id = id; | ||
model.commit(); | ||
model.calculate().done(); | ||
return model.ready(); | ||
}); | ||
} else { | ||
return model.ready().then(function () { | ||
return model.storage.update(model); | ||
}).then(function () { | ||
model.commit(); | ||
}); | ||
} | ||
return this._rejectDestructed().then(function () { | ||
if (model.isNew()) { | ||
return model.ready().then(function () { | ||
return model.storage.insert(model); | ||
}).then(function (id) { | ||
model.id = id; | ||
model.commit(); | ||
model.calculate().done(); | ||
return model.ready(); | ||
}); | ||
} else { | ||
return model.ready().then(function () { | ||
return model.storage.update(model); | ||
}).then(function () { | ||
model.commit(); | ||
}); | ||
} | ||
}); | ||
}, | ||
@@ -214,3 +214,5 @@ | ||
Object.keys(data).forEach(function (name) { | ||
model.set(name, data[name]); | ||
if (data[name] !== undefined) { | ||
model.set(name, data[name]); | ||
} | ||
}); | ||
@@ -257,5 +259,7 @@ } else if (this.attributes[name]) { | ||
* @param {*} [ctx] | ||
* @return {Model} | ||
*/ | ||
un: function (attribute, event, cb, ctx) { | ||
this._callEventEmitter('removeListener', attribute, event, cb, ctx); | ||
return this; | ||
}, | ||
@@ -269,5 +273,7 @@ | ||
* @param {*} [ctx] | ||
* @return {Model} | ||
*/ | ||
on: function (attribute, event, cb, ctx) { | ||
this._callEventEmitter('on', attribute, event, cb, ctx); | ||
return this; | ||
}, | ||
@@ -314,2 +320,4 @@ | ||
this._readyPromise = this._calculate(); | ||
} else { | ||
this._requireMoreCalculations = true; | ||
} | ||
@@ -333,2 +341,8 @@ if (this.throwCalculationErrors) { | ||
/** | ||
* marker that requires one more calculation cycle | ||
* @type {Boolean} | ||
*/ | ||
_requireMoreCalculations: false, | ||
/** | ||
* @param {Number} [n = 0] itteration | ||
@@ -368,3 +382,4 @@ * @return {Promise} | ||
model.set(calculateData); | ||
if (model.isChanged(model.CALCULATIONS_BRANCH)) { | ||
if (model.isChanged(model.CALCULATIONS_BRANCH) || model._requireMoreCalculations) { | ||
model._requireMoreCalculations = false; | ||
return model._calculate(++n); | ||
@@ -452,2 +467,13 @@ } else { | ||
/** | ||
* @return {Promise} | ||
*/ | ||
_rejectDestructed: function () { | ||
if (this.isDestructed()) { | ||
return Vow.reject(new Error ('Model is destructed')); | ||
} else { | ||
return Vow.fulfill(); | ||
} | ||
}, | ||
_throwMissedAttribute: function (attributeName) { | ||
@@ -462,2 +488,12 @@ if (!this.attributes[attributeName]) { | ||
/** | ||
* @override | ||
*/ | ||
inherit: function (props, staticProps) { | ||
staticProps = staticProps || {}; | ||
staticProps.attributes = staticProps.attributes || props.attributes; | ||
staticProps.storage = staticProps.storage || props.storage; | ||
return this.__base(props, staticProps); | ||
}, | ||
/** | ||
* @class | ||
@@ -464,0 +500,0 @@ * @abstract |
{ | ||
"description": "promise based, typed attributes, nested models and collections", | ||
"name": "promised-models", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"repository": "git@github.com:delfrrr/promised-models.git", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -221,4 +221,4 @@ # Promised Models | ||
```js | ||
model.on('change', this.changeHandler, this); | ||
model.on('change:weight change:name', this.changeHandler, this); | ||
model.on('change', this.changeHandler, this) | ||
.on('change:weight change:name', this.changeHandler, this); | ||
``` | ||
@@ -390,2 +390,14 @@ | ||
#### Storage `Model.storage` | ||
Storage class | ||
```js | ||
var SuperModel = FashionModel.inherit({ | ||
storage: FashionModel.storage.inherit({ //extend storage from FashionModel | ||
//.. | ||
}) | ||
}); | ||
``` | ||
#### Attribute `Model.Attribute` | ||
@@ -401,2 +413,17 @@ | ||
#### Attribute `Model.attributes` | ||
Model class attributes | ||
```js | ||
var SuperModel = FashionModel.inherit({ | ||
attributes: { | ||
name: FashionModel.attributes.name, | ||
weight: FashionModel.attributes.weight.inherit({ | ||
default: 50 | ||
}) | ||
} | ||
}); | ||
``` | ||
### List | ||
@@ -403,0 +430,0 @@ |
@@ -7,8 +7,18 @@ var expect = require('chai').expect; | ||
var ModelClass = require('./models/simple'), | ||
model; | ||
beforeEach(function () { | ||
model = new ModelClass({ | ||
a: 'a1' | ||
}); | ||
}); | ||
it('should get init values', function () { | ||
expect(model.get('a')).to.be.equal('a1'); | ||
}); | ||
it('should not set undefined values', function () { | ||
var model = new ModelClass({ | ||
a: undefined, | ||
b: 'b-1' | ||
}); | ||
expect(model.get('a')).to.be.equal('a'); | ||
}); | ||
}); | ||
@@ -63,3 +73,7 @@ }); | ||
var ModelClass = require('./models/simple'), | ||
model; | ||
beforeEach(function () { | ||
model = new ModelClass(); | ||
}); | ||
@@ -82,2 +96,9 @@ it('should set value', function () { | ||
}); | ||
it('should not set undefined fields', function () { | ||
model.set({ | ||
a: undefined, | ||
b: 'b-1' | ||
}); | ||
expect(model.get('a')).to.be.equal('a'); | ||
}); | ||
}); | ||
@@ -84,0 +105,0 @@ |
@@ -31,2 +31,6 @@ | ||
}); | ||
it('should return model instance', function () { | ||
var model = new ModelClass(); | ||
expect(model.on('change', function () {})).to.be.equal(model); | ||
}); | ||
}); | ||
@@ -48,2 +52,6 @@ describe('Models.un', function () { | ||
}); | ||
it('should return model instance', function () { | ||
var model = new ModelClass(); | ||
expect(model.un('change', function () {})).to.be.equal(model); | ||
}); | ||
}); | ||
@@ -50,0 +58,0 @@ describe('change', function () { |
var expect = require('chai').expect; | ||
var expect = require('chai').expect, | ||
Vow = require('vow'); | ||
@@ -48,2 +49,17 @@ describe('Persistent', function () { | ||
describe('remove', function () { | ||
it('should prevent further save', function (done) { | ||
var model = new Persistent(); | ||
model.save().then(function () { | ||
model.remove(); | ||
return model.save().fail(function (err) { | ||
expect(err.message).to.have.string('destructed'); | ||
return Vow.fulfill(); | ||
}); | ||
}).then(function () { | ||
done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('collection save', function () { | ||
@@ -50,0 +66,0 @@ it('should call calculate', 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
97357
36
2485
479