promised-models
Advanced tools
Comparing version 0.0.18 to 0.0.19
@@ -51,4 +51,4 @@ /** | ||
remove: function () { | ||
throw new Error('.find() is not implemented'); | ||
throw new Error('.remove() is not implemented'); | ||
} | ||
}); |
@@ -16,2 +16,3 @@ /** | ||
__constructor: function () { | ||
this.LISTEN_BRANCH = 'LISTEN_BRANCH'; | ||
this.__base.apply(this, arguments); | ||
@@ -96,10 +97,28 @@ this._initModel(); | ||
_onModelChange: function () { | ||
this._emitChange(); | ||
this._emitChange(true); | ||
}, | ||
/** | ||
* @override | ||
* @param {Boolean} [fromNestedModel=true] do not init listeners | ||
*/ | ||
_emitChange: function (fromNestedModel) { | ||
if (!fromNestedModel) { | ||
//model changed we need to subscribe again | ||
this._initModel(); | ||
} | ||
this.__base(); | ||
}, | ||
/** | ||
* bind to model events | ||
*/ | ||
_initModel: function () { | ||
var prevValue = this._cachBranches[this.LISTEN_BRANCH]; | ||
if (prevValue) { | ||
//unsubscribing | ||
prevValue.un('calculate', this._onModelChange, this); | ||
} | ||
this.value.on('calculate', this._onModelChange, this); | ||
this.commit(this.LISTEN_BRANCH); | ||
} | ||
@@ -106,0 +125,0 @@ |
{ | ||
"description": "promise based, typed attributes, nested models and collections", | ||
"name": "promised-models", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"repository": "git@github.com:delfrrr/promised-models.git", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
var expect = require('chai').expect; | ||
describe('Nested models', function () { | ||
var Model = require('./models/with-nested'); | ||
var ModelWithNested = require('./models/with-nested'), | ||
Model = require('../lib/model'); | ||
describe('model attribute', function () { | ||
@@ -15,3 +16,3 @@ var data, model; | ||
}; | ||
model = new Model(data); | ||
model = new ModelWithNested(data); | ||
return model.ready(); | ||
@@ -22,13 +23,50 @@ }); | ||
}); | ||
it('should trigger change on parent model', function (done) { | ||
model.on('change', function () { | ||
done(); | ||
describe('change', function () { | ||
it('should be triggered on parent model', function (done) { | ||
model.on('change', function () { | ||
done(); | ||
}); | ||
model.get('nested').set('a', 'a-1'); | ||
}); | ||
model.get('nested').set('a', 'a-1'); | ||
}); | ||
it('should trigger change:attribute on parent model', function (done) { | ||
model.on('change:nested', function () { | ||
done(); | ||
describe(':attribute', function () { | ||
it('should triggered on parent model', function (done) { | ||
model.on('change:nested', function () { | ||
done(); | ||
}); | ||
model.get('nested').set('a', 'a-1'); | ||
}); | ||
}); | ||
model.get('nested').set('a', 'a-1'); | ||
it('should be triggered on parent after replacement of nested model', function (done) { | ||
var Nested = Model.inherit({ | ||
attributes: { | ||
a: Model.attributeTypes.String.inherit({ | ||
default: 'a0' | ||
}) | ||
} | ||
}), | ||
Parent = Model.inherit({ | ||
attributes: { | ||
nested: Model.attributeTypes.Model(Nested), | ||
aNested: Model.attributeTypes.String.inherit({ | ||
calculate: function () { | ||
return this.model.get('nested').get('a'); | ||
} | ||
}) | ||
} | ||
}), | ||
parent = new Parent(), nested = new Nested({ | ||
a: 'a1' | ||
}); | ||
parent.ready().then(function () { | ||
parent.set('nested', nested); | ||
return parent.ready(); | ||
}).then(function () { | ||
parent.on('change', function () { | ||
expect(parent.get('aNested')).to.be.equal('a2'); | ||
done(); | ||
}); | ||
parent.get('nested').set('a', 'a2'); | ||
}).done(); | ||
}); | ||
}); | ||
@@ -35,0 +73,0 @@ it('should validate', function (done) { |
108427
2787