promised-models2
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -167,2 +167,9 @@ | ||
/** | ||
* Destructor, default implementation | ||
* does nothing | ||
*/ | ||
destruct: function () { | ||
}, | ||
/** | ||
* get attribute value | ||
@@ -169,0 +176,0 @@ * @return {*} |
@@ -62,2 +62,9 @@ var Events = require('./events'), | ||
destruct: function () { | ||
(this._models || []).forEach(function (model) { | ||
model.un('all', this._onModelEvent, this); | ||
}, this); | ||
return this.__base.apply(this, arguments); | ||
}, | ||
/** | ||
@@ -64,0 +71,0 @@ * @param {String} [branch=DEFAULT_BRANCH] |
@@ -209,2 +209,5 @@ /** | ||
this._eventEmitter.removeAllListeners(); | ||
Object.keys(this.attributes).forEach(function (attributeName) { | ||
this.attributes[attributeName].destruct(); | ||
}, this); | ||
}, | ||
@@ -211,0 +214,0 @@ |
@@ -107,29 +107,34 @@ /** | ||
}, | ||
/** | ||
* @override {Attribute} | ||
*/ | ||
isEqual: function (value) { | ||
return this.value === value; | ||
}, | ||
/** | ||
isEqual: function (value) { | ||
return this.value === value; | ||
}, | ||
/** | ||
* @override {Attribute} | ||
* set attribute value | ||
* @param {*} value | ||
*/ | ||
set: function (value) { | ||
if (value === null) { | ||
this.unset(); | ||
} else if (!this.isEqual(value)) { | ||
if (value instanceof this.modelType) { | ||
this.value = value; | ||
} else { | ||
this.value.set(value); | ||
} | ||
this._isSet = true; | ||
this._emitChange(); | ||
} | ||
}, | ||
* set attribute value | ||
* @param {*} value | ||
*/ | ||
set: function (value) { | ||
if (value === null) { | ||
this.unset(); | ||
} else if (!this.isEqual(value)) { | ||
if (value instanceof this.modelType) { | ||
this.value = value; | ||
} else { | ||
this.value.set(value); | ||
} | ||
this._isSet = true; | ||
this._emitChange(); | ||
} | ||
}, | ||
destruct: function () { | ||
this.value && this.value.un('calculate', this._onModelChange, this); | ||
return this.__base.apply(this, arguments); | ||
}, | ||
/** | ||
@@ -136,0 +141,0 @@ * @override {Attribute} |
@@ -31,2 +31,10 @@ /** | ||
destruct: function () { | ||
this.value.forEach(function (model) { | ||
model.un('calculate', this._onModelCalculate, this); | ||
model.un('destruct', this._onModelDestruct, this); | ||
}, this); | ||
return this.__base.apply(this, arguments); | ||
}, | ||
default: [], | ||
@@ -33,0 +41,0 @@ |
{ | ||
"description": "promise based, typed attributes, nested models and collections", | ||
"name": "promised-models2", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"repository": "git@github.com:bem-node/promised-models.git", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -404,3 +404,22 @@ var expect = require('chai').expect; | ||
}); | ||
describe('destruct', function () { | ||
it ('should unsubscribe from nested models', function (done) { | ||
var oldModelEventHandler = collection._onModelEvent, | ||
model = new TestModel({id: 555, a: '123'}), | ||
flag = false; | ||
collection._onModelEvent = function () { | ||
flag = true; | ||
return oldModelEventHandler.apply(this, arguments); | ||
}; | ||
collection.add(model, {at: 0}); | ||
collection.destruct(); | ||
collection.at(0).set('a', 'zzzzz'); | ||
model.ready().then(function () { | ||
expect(flag).to.be.equal(false); | ||
done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('model events', function () { | ||
@@ -407,0 +426,0 @@ it('should trigger change event', function (done) { |
@@ -189,4 +189,21 @@ var expect = require('chai').expect; | ||
}); | ||
it('should unsubscribe from nested models', function (done) { | ||
var oldCalculate = model.calculate, | ||
nested = model.get('collection').get(0), | ||
flag = false; | ||
model.calculate = function () { | ||
flag = true; | ||
return oldCalculate.apply(this, arguments); | ||
}; | ||
model.ready().then(function () { | ||
model.destruct(); | ||
flag = false; | ||
nested.set('a', '123'); | ||
expect(flag).to.be.equal(false); | ||
done(); | ||
}).done(); | ||
}); | ||
}); | ||
}); |
@@ -108,2 +108,21 @@ var expect = require('chai').expect; | ||
}); | ||
describe('destruct', function () { | ||
it ('should unsubscribe from nested model on destruct', function (done) { | ||
var oldCalculate = model.calculate, | ||
nested = model.get('nested'), | ||
flag = false; | ||
model.calculate = function () { | ||
flag = true; | ||
return oldCalculate.apply(this, arguments); | ||
}; | ||
model.ready().then(function () { | ||
model.destruct(); | ||
flag = false; | ||
nested.set('a', '123'); | ||
expect(flag).to.be.equal(false); | ||
done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('revert', function () { | ||
@@ -110,0 +129,0 @@ it('should revert nested model', 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
171731
4429