promised-models
Advanced tools
Comparing version 0.0.12 to 0.0.14
@@ -11,9 +11,14 @@ | ||
__constructor: function (initValue) { | ||
var setValue; | ||
this._cachBranches = {}; | ||
this._cachIsSetBranches = {}; | ||
this.DEFAULT_BRANCH = 'DEFAULT_BRANCH'; | ||
this.value = this.parse( | ||
initValue === undefined ? | ||
this._callOrGetDefault() : | ||
initValue | ||
); | ||
if (initValue === undefined) { | ||
this._isSet = false; | ||
setValue = this._callOrGetDefault(); | ||
} else { | ||
this._isSet = true; | ||
setValue = initValue; | ||
} | ||
this.value = this.parse(setValue); | ||
this.commit(); | ||
@@ -23,2 +28,20 @@ }, | ||
/** | ||
* check if attribute was set | ||
* @param {string} attributeName | ||
* @return {Boolean} | ||
*/ | ||
isSet: function () { | ||
return this._isSet; | ||
}, | ||
/** | ||
* set attribute to default value | ||
* @param {string} attributeName | ||
*/ | ||
unset: function () { | ||
this._isSet = false; | ||
this.value = this._callOrGetDefault(); | ||
}, | ||
/** | ||
* check if attribute is valid | ||
@@ -67,2 +90,3 @@ * @abstract | ||
this.value = this._cachBranches[branch]; | ||
this._isSet = this._cachIsSetBranches[branch]; | ||
this._emitChange(); | ||
@@ -79,2 +103,3 @@ } | ||
this._cachBranches[branch] = this.value; | ||
this._cachIsSetBranches[branch] = this._isSet; | ||
}, | ||
@@ -89,2 +114,3 @@ | ||
this.value = this.parse(value); | ||
this._isSet = true; | ||
this._emitChange(); | ||
@@ -91,0 +117,0 @@ } |
@@ -47,2 +47,21 @@ /** | ||
/** | ||
* set attribute to default value | ||
* @param {string} attributeName | ||
*/ | ||
unset: function (attributeName) { | ||
this._throwMissedAttribute(attributeName); | ||
this.attributes[attributeName].unset(); | ||
}, | ||
/** | ||
* check if attribute was set | ||
* @param {string} attributeName | ||
* @return {Boolean} | ||
*/ | ||
isSet: function (attributeName) { | ||
this._throwMissedAttribute(attributeName); | ||
return this.attributes[attributeName].isSet(); | ||
}, | ||
/** | ||
* when false calculation errors will be silent | ||
@@ -49,0 +68,0 @@ * @type {Boolean} |
@@ -21,2 +21,16 @@ /** | ||
/** | ||
* @override {Attribute} | ||
*/ | ||
isSet: function () { | ||
throw new Error('.isSet is not implemented for nested models'); | ||
}, | ||
/** | ||
* @override {Attribute} | ||
*/ | ||
unset: function () { | ||
throw new Error('.unset is not implemented for nested models'); | ||
}, | ||
/** | ||
* nested model ready | ||
@@ -54,3 +68,3 @@ * @return {Promise} | ||
revert: function (branch) { | ||
return this.value.commit(branch); | ||
return this.value.revert(branch); | ||
}, | ||
@@ -57,0 +71,0 @@ |
@@ -38,2 +38,16 @@ /** | ||
*/ | ||
isSet: function () { | ||
throw new Error('.isSet is not implemented for nested models'); | ||
}, | ||
/** | ||
* @override {Attribute} | ||
*/ | ||
unset: function () { | ||
throw new Error('.unset is not implemented for nested models'); | ||
}, | ||
/** | ||
* @override {Attribute} | ||
*/ | ||
get: function () { | ||
@@ -40,0 +54,0 @@ return (new List(this)); |
{ | ||
"description": "promise based, typed attributes, nested models and collections", | ||
"name": "promised-models", | ||
"version": "0.0.12", | ||
"version": "0.0.14", | ||
"repository": "git@github.com:delfrrr/promised-models.git", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -209,3 +209,3 @@ # Promised Models | ||
#### on `model.on(events, cb, [ctx])` | ||
#### on `model.on([attributes], events, cb, [ctx])` | ||
@@ -226,3 +226,3 @@ Add event handler for one or multiple model events. | ||
#### un `model.un(events, cb, [ctx])` | ||
#### un `model.un([attributes], events, cb, [ctx])` | ||
@@ -233,3 +233,3 @@ Unsubscribe event handler from events. | ||
//subscribe | ||
model.on('change:weight change:name', this.changeHandler, this); | ||
model.on('weight name', 'change', this.changeHandler, this); | ||
@@ -244,2 +244,25 @@ //unsubscribe | ||
#### isSet `model.isSet(attributeName)` | ||
Returns `true` if attribute was set via constructor or set | ||
```js | ||
var model = new FashionModel(); | ||
model.isSet('name'); //false | ||
model.set('name', 'Kate'); | ||
model.isSet('name'); //true | ||
``` | ||
#### unset `model.unset(attributeName)` | ||
Set attribute to default value and `model.isSet() === 'false'` | ||
```js | ||
var model = new FashionModel(); | ||
model.set('name', 'Kate'); | ||
model.unset('name'); | ||
model.isSet('name'); //false | ||
model.get('name'); //empty string (default value) | ||
``` | ||
### Model async methods | ||
@@ -393,3 +416,3 @@ | ||
#### Storage `Model.storage` | ||
#### Class storage `Model.storage` | ||
@@ -416,3 +439,3 @@ Storage class | ||
#### Attribute `Model.attributes` | ||
#### Class attributes `Model.attributes` | ||
@@ -419,0 +442,0 @@ Model class attributes |
@@ -69,3 +69,18 @@ var expect = require('chai').expect; | ||
}); | ||
describe('revert', function () { | ||
it('should revert nested model', function () { | ||
model.get('nested').set('a', 'a-1'); | ||
model.revert(); | ||
expect(model.get('nested').get('a')).to.be.equal('a-0'); | ||
}); | ||
}); | ||
describe('commit', function () { | ||
it('should cache model state', function () { | ||
model.get('nested').set('a', 'a-1'); | ||
model.commit(); | ||
model.revert(); | ||
expect(model.get('nested').get('a')).to.be.equal('a-1'); | ||
}); | ||
}); | ||
}); | ||
}); |
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
102752
37
2632
502