promised-models2
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -214,6 +214,5 @@ | ||
* @name Attribute#amend | ||
* @return {Promise} when done | ||
*/ | ||
_emitChange: function () { | ||
this.model.calculate().done(); | ||
this.model.calculate(); | ||
}, | ||
@@ -220,0 +219,0 @@ |
@@ -37,2 +37,6 @@ var Events = require('./events'), | ||
if (!this.modelType.prototype.attributes.id) { | ||
throw new Error ('Collection can not be used with models without identifier attribute'); | ||
} | ||
/** | ||
@@ -370,3 +374,3 @@ * @type {Array.<Model>} | ||
if (eventName === 'change:' + model.idAttribute.name) { | ||
if (model.idAttribute && eventName === 'change:' + model.idAttribute.name) { | ||
if (model.idAttribute.previous() !== null) { | ||
@@ -373,0 +377,0 @@ delete this._modelsIdsMap[model.idAttribute.previous()]; |
@@ -30,3 +30,2 @@ /** | ||
Storage = this.storage || Model.Storage, | ||
hasIdAttribute, | ||
modelAttributes; | ||
@@ -52,12 +51,5 @@ | ||
modelAttributes = this.attributes || {}; | ||
hasIdAttribute = Object.keys(modelAttributes).some(function (name) { | ||
return modelAttributes[name].prototype.isId; | ||
}); | ||
if (!hasIdAttribute) { | ||
modelAttributes.id = IdAttribute; | ||
} | ||
// todo: remove after removing id property support | ||
if (id !== null) { | ||
if (id !== null && modelAttributes.id) { | ||
data.id = id; | ||
@@ -82,7 +74,8 @@ } | ||
// todo: remove after removing id property support | ||
this.id = this.idAttribute.get(); | ||
this.on('change:' + this.idAttribute.name, function () { | ||
this.id = this.idAttribute.get(); | ||
}, this); | ||
if (this.idAttribute) { | ||
this.id = this.getId(); | ||
this.on('change:' + this.idAttribute.name, function () { | ||
this.id = this.idAttribute.get(); | ||
}, this); | ||
} | ||
this._attributesAr = Object.keys(this.attributes).map(function (name) { | ||
@@ -92,3 +85,3 @@ return model.attributes[name]; | ||
this.commit(this.CHANGE_BRANCH); | ||
this.calculate().done(); | ||
this.calculate(); | ||
}, | ||
@@ -100,3 +93,3 @@ | ||
getId: function () { | ||
return this.idAttribute.get(); | ||
return this.idAttribute ? this.idAttribute.get() : null | ||
}, | ||
@@ -143,2 +136,5 @@ | ||
var model = this; | ||
if (!model.idAttribute) { | ||
throw new Error('model without declared perisitent id attribute cat not be saved'); | ||
} | ||
return this._rejectDestructed().then(function () { | ||
@@ -151,3 +147,3 @@ if (model.isNew()) { | ||
model.commit(); | ||
model.calculate().done(); | ||
model.calculate(); | ||
return model.ready(); | ||
@@ -171,2 +167,5 @@ }); | ||
var model = this; | ||
if (!model.idAttribute) { | ||
throw new Error('model can not be fetched from persistent storage, if it has no persistent id'); | ||
} | ||
return this.ready().then(function () { | ||
@@ -192,2 +191,5 @@ return model.storage.find(model); | ||
} else { | ||
if (!model.idAttribute) { | ||
throw new Error('model can not be removed from persistet storage, if it has no persistent id'); | ||
} | ||
return fulfill().then(function () { | ||
@@ -406,5 +408,6 @@ return model.storage.remove(model); | ||
}); | ||
this._readyPromise.fail(function () { | ||
this._readyPromise.fail(function (e) { | ||
console.error(e, e && e.stack) | ||
model._ready = true; | ||
}).done(); | ||
}); | ||
} else { | ||
@@ -411,0 +414,0 @@ this._requireMoreCalculations = true; |
{ | ||
"description": "promise based, typed attributes, nested models and collections", | ||
"name": "promised-models2", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"repository": "git@github.com:bem-node/promised-models.git", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -159,3 +159,3 @@ Promised Models 2 [](https://travis-ci.org/bem-node/promised-models) | ||
Returns entity id. You can declare special id attribute, which will be interpreted as entity id. If id attribute didn't declared, by default model will add declaration with name `id` | ||
Returns entity id. You can declare special id attribute, which will be interpreted as entity id. If id attribute is not declared, getId returns null | ||
@@ -455,2 +455,3 @@ ```js | ||
attributes: { | ||
id: Model.attributeTypes.Id, | ||
name: Model.attributeTypes.String, | ||
@@ -619,3 +620,3 @@ weight: Model.attributeTypes.Number | ||
Creates you own collection class by extending Collection. You should define `modelType` property - constructor which will be used for new models. | ||
Creates you own collection class by extending Collection. You should define `modelType` property - constructor which will be used for new models. Models that are used in collections should have declared id attribute, to make getId and related methods to work correctly. | ||
@@ -622,0 +623,0 @@ ```js |
@@ -22,2 +22,3 @@ var expect = require('chai').expect; | ||
attributes: { | ||
id: Model.attributeTypes.Id, | ||
a: Model.attributeTypes.String | ||
@@ -40,2 +41,13 @@ }, | ||
}); | ||
describe('internal model', function () { | ||
it('should throw exception if model has no id attribute', function () { | ||
var ModelWithoutId = Model; | ||
expect(function () { | ||
new (Collection.inherit({ | ||
modelType: ModelWithoutId | ||
})); | ||
}).to.throw(Error); | ||
}); | ||
}); | ||
@@ -42,0 +54,0 @@ describe('length', function () { |
var expect = require('chai').expect, | ||
Model = require('../lib/model'); | ||
Model = require('../lib/model'), | ||
TestModel = Model.inherit({attributes: { | ||
id: Model.attributeTypes.Id | ||
}}); | ||
@@ -8,10 +11,4 @@ describe('Id attribute', function () { | ||
it('should add id attribute if it absent in attributes declaration', function () { | ||
var model = new Model(); | ||
expect(model.attributes.id).to.be.an.instanceof(Model.attributeTypes.Id); | ||
expect(model.getId()).to.be.null; | ||
}); | ||
it('should get "id" property as id attribute from initial data', function () { | ||
var model = new Model({ | ||
var model = new TestModel({ | ||
id: 1 | ||
@@ -61,3 +58,3 @@ }); | ||
it('should correctly check equality with null', function () { | ||
var model = new Model(); | ||
var model = new TestModel(); | ||
expect(model.attributes.id.isEqual(null)).to.be.true; | ||
@@ -67,3 +64,3 @@ }); | ||
it('should correctly check equality with non-null', function () { | ||
var model = new Model({ | ||
var model = new TestModel({ | ||
id: 1 | ||
@@ -80,6 +77,6 @@ }); | ||
it('should support old interface', function () { | ||
var model = new Model(1); | ||
var model = new TestModel(1); | ||
expect(model.getId()).to.equal(1); | ||
model = new Model({ | ||
model = new TestModel({ | ||
id: 1 | ||
@@ -91,3 +88,3 @@ }); | ||
it('should change id property when id attribute has changed', function () { | ||
var model = new Model(1); | ||
var model = new TestModel(1); | ||
model.set('id', 2); | ||
@@ -94,0 +91,0 @@ model.ready().then(function () { |
@@ -27,2 +27,3 @@ var expect = require('chai').expect, | ||
attributes: { | ||
id: Model.attributeTypes.Id, | ||
b: ModelClass1.attributes.b, | ||
@@ -56,5 +57,5 @@ c: Model.attributeTypes.String.inherit({ | ||
expect(model2.toJSON()).to.be.deep.equal({ | ||
id: null, | ||
b: 'b-1', | ||
c: 'c-1' | ||
c: 'c-1', | ||
id: null | ||
}); | ||
@@ -61,0 +62,0 @@ }); |
@@ -58,3 +58,2 @@ var expect = require('chai').expect; | ||
expect(model.toJSON()).to.deep.equal({ | ||
id: null, | ||
list: arr | ||
@@ -61,0 +60,0 @@ }); |
@@ -10,2 +10,3 @@ /** | ||
attributes: { | ||
id: Model.attributeTypes.Id, | ||
a: Model.attributeTypes.String.inherit({ | ||
@@ -12,0 +13,0 @@ default: 'a-0' |
@@ -8,2 +8,3 @@ /** | ||
attributes: { | ||
id: Model.attributeTypes.Id, | ||
a: Model.attributeTypes.String, | ||
@@ -10,0 +11,0 @@ b: Model.attributeTypes.String, |
@@ -1,2 +0,1 @@ | ||
var expect = require('chai').expect, | ||
@@ -38,2 +37,3 @@ Vow = require('vow'); | ||
attributes: { | ||
id: Model.attributeTypes.Id, | ||
a: Model.attributeTypes.String.inherit({ | ||
@@ -40,0 +40,0 @@ default: 'a' |
@@ -9,2 +9,3 @@ var expect = require('chai').expect; | ||
attributes: { | ||
id: Model.attributeTypes.Id, | ||
a: Model.attributeTypes.String | ||
@@ -11,0 +12,0 @@ } |
170651
4351
708