Comparing version 0.2.8 to 0.2.9
{ | ||
"name": "brink", | ||
"version": "0.2.8", | ||
"version": "0.2.9", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./src/brink/brink.js", |
@@ -357,1 +357,50 @@ # brink.js | ||
$ node tasks/test | ||
#### Contributors | ||
- [Taka Kojima][gigafied] | ||
- [Patrick Weygand][derduher] | ||
[gigafied]: https://github.com/gigafied | ||
[derduher]: https://github.com/derduher | ||
#### License | ||
This software is released under the terms of the MIT License. | ||
(c) 2015 Taka Kojima (the "Author"). | ||
All Rights Reserved. | ||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
Distributions of all or part of the Software intended to be used | ||
by the recipients as they would use the unmodified Software, | ||
containing modifications that substantially alter, remove, or | ||
disable functionality of the Software, outside of the documented | ||
configuration mechanisms provided by the Software, shall be | ||
modified such that the Author's bug reporting email addresses and | ||
urls are either replaced with the contact information of the | ||
parties responsible for the changes, or removed entirely. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
Except where noted, this license applies to any and all software | ||
programs and associated documentation files created by the | ||
Author, when distributed with the Software. |
@@ -91,4 +91,9 @@ $b( | ||
serialize : function () { | ||
return get(this, attr.meta().key); | ||
serialize : function (filter) { | ||
var meta = attr.meta(), | ||
k = meta.key, | ||
v = get(this, k); | ||
if (!filter || filter(meta, k, v)) { | ||
return v; | ||
} | ||
}, | ||
@@ -122,2 +127,2 @@ | ||
).attach('$b'); | ||
).attach('$b'); |
@@ -90,3 +90,3 @@ $b( | ||
serialize : function () { | ||
serialize : function (filter) { | ||
@@ -105,12 +105,16 @@ var key, | ||
if (options.embedded) { | ||
return val.serialize(); | ||
val = val.serialize(filter); | ||
} else { | ||
val = get(val, 'pk'); | ||
} | ||
return get(val, 'pk'); | ||
} | ||
return val; | ||
if (!filter || filter(meta, key, val)) { | ||
return val; | ||
} | ||
}, | ||
deserialize : function (val, override) { | ||
deserialize : function (val, override, filter) { | ||
@@ -128,3 +132,3 @@ var key, | ||
if (val && typeof val === 'object') { | ||
val = record.deserialize(val, override); | ||
val = record.deserialize(val, override, filter); | ||
} | ||
@@ -159,2 +163,2 @@ } | ||
).attach('$b'); | ||
).attach('$b'); |
@@ -83,3 +83,3 @@ $b( | ||
serialize : function (isEmbedded) { | ||
serialize : function (isEmbedded, filter) { | ||
@@ -91,3 +91,3 @@ var a = []; | ||
if (isEmbedded) { | ||
a.push(item.serialize()); | ||
a.push(item.serialize(filter)); | ||
} | ||
@@ -128,2 +128,2 @@ | ||
).attach('$b'); | ||
).attach('$b'); |
@@ -24,2 +24,5 @@ $b( | ||
get : function (key) { | ||
if (!this.__meta.data[key]) { | ||
this.__meta.data[key] = Collection.create(); | ||
} | ||
return this.__meta.data[key]; | ||
@@ -89,3 +92,3 @@ }, | ||
serialize : function () { | ||
serialize : function (filter) { | ||
@@ -106,3 +109,3 @@ var i, | ||
if (val) { | ||
val = val.serialize(options.embedded); | ||
val = val.serialize(options.embedded, filter); | ||
} | ||
@@ -129,6 +132,9 @@ | ||
return val; | ||
if (!filter || filter(meta, key, val)) { | ||
return val; | ||
} | ||
}, | ||
deserialize : function (val) { | ||
deserialize : function (val, override, filter) { | ||
@@ -196,3 +202,3 @@ var i, | ||
record.deserialize(val[i]); | ||
record.deserialize(val[i], override, filter); | ||
} | ||
@@ -250,2 +256,2 @@ | ||
).attach('$b'); | ||
).attach('$b'); |
@@ -131,3 +131,3 @@ $b( | ||
serialize : function () { | ||
serialize : function (filter) { | ||
@@ -163,3 +163,3 @@ var i, | ||
val = pMeta.serialize.call(this); | ||
val = pMeta.serialize.call(this, filter); | ||
if (typeof val !== 'undefined') { | ||
@@ -180,3 +180,3 @@ set(json, key, val); | ||
deserialize : function (json, override) { | ||
deserialize : function (json, override, filter) { | ||
@@ -220,4 +220,4 @@ var i, | ||
if (typeof val !== 'undefined') { | ||
val = pMeta.deserialize.call(this, val, override); | ||
if (typeof val !== 'undefined' && (!filter || filter(pMeta, key, val))) { | ||
val = pMeta.deserialize.call(this, val, override, filter); | ||
meta.pristineData[p] = val; | ||
@@ -224,0 +224,0 @@ } |
@@ -85,3 +85,3 @@ $b( | ||
record = records[i]; | ||
record = record.__meta.controller || record; | ||
record = record.getController() || record; | ||
collection.remove(records[i]); | ||
@@ -164,3 +164,3 @@ } | ||
this.add(mKey, record); | ||
record = record.__meta.controller || record; | ||
record = record.getController() || record; | ||
} | ||
@@ -171,2 +171,14 @@ | ||
createRecord : function (mKey, data, wrap) { | ||
record = this.modelFor(mKey).create(data); | ||
this.add(mKey, record); | ||
if (wrap === false) { | ||
return record; | ||
} | ||
return record.getController() || record; | ||
}, | ||
filter : function (mKey, q) { | ||
@@ -173,0 +185,0 @@ |
@@ -186,2 +186,64 @@ describe('belongsTo', function () { | ||
}); | ||
it('should allow filtering.', function () { | ||
var json, | ||
Light, | ||
LightSwitch, | ||
switchInstance; | ||
Light = $b.Model({ | ||
modelKey : 'light', | ||
isOn : $b.attr({defaultValue : false}), | ||
isDimmable : $b.attr({defaultValue : false, internal : true}), | ||
voltage : $b.attr({readOnly: true}) | ||
}); | ||
LightSwitch = $b.Model({ | ||
modelKey : 'lightSwitch', | ||
collectionKey : 'lightSwitches', | ||
light : $b.belongsTo('light', {embedded : true}), | ||
linkedTo : $b.belongsTo('light', {embedded: true, readOnly : true}), | ||
flip : function () { | ||
this.light.isOn = !this.light.isOn; | ||
} | ||
}); | ||
switchInstance = LightSwitch.create(); | ||
store.add('lightSwitch', switchInstance); | ||
switchInstance.deserialize({ | ||
light : { | ||
isOn : true, | ||
voltage: 1.2 | ||
}, | ||
linkedTo: {isOn: true, voltage: 123} | ||
}, false, function (meta) { | ||
return !meta.options.readOnly; | ||
}); | ||
switchInstance.flip(); | ||
expect(switchInstance.linkedTo).to.equal(undefined); | ||
switchInstance.linkedTo = Light.create({voltage: 345}); | ||
json = switchInstance.serialize(function (meta) { | ||
return !meta.options.internal; | ||
}); | ||
expect(json).to.deep.equal({ | ||
light : { | ||
isOn : false | ||
}, | ||
linkedTo: { | ||
isOn: false, | ||
voltage: 345 | ||
} | ||
}); | ||
Light.unregister(); | ||
LightSwitch.unregister(); | ||
}); | ||
}); |
@@ -172,2 +172,31 @@ describe('deserialize', function () { | ||
it('should apply filterers to properties', function () { | ||
var json, | ||
Model, | ||
instance; | ||
Model = $b.Model({ | ||
a : $b.attr({key : 'a.b.c.d', readOnly: true}) | ||
}); | ||
json = { | ||
a : { | ||
b : { | ||
c : { | ||
d : 'test' | ||
} | ||
} | ||
} | ||
}; | ||
instance = Model.create(); | ||
instance.deserialize(json, false, function (meta) { | ||
return !meta.options.readOnly; | ||
}); | ||
expect(instance.serialize()).to.deep.equal({}); | ||
instance.destroy(); | ||
}); | ||
}); |
@@ -7,3 +7,5 @@ describe('hasMany', function () { | ||
Comment, | ||
Permission; | ||
Permission, | ||
emptyPostJSON, | ||
emptyCommentJSON; | ||
@@ -21,3 +23,5 @@ before(function () { | ||
author : $b.belongsTo('user'), | ||
likes : $b.hasMany('user') | ||
views: $b.hasMany('user', {internal: true}), | ||
likes : $b.hasMany('user'), | ||
showTo : $b.hasMany('user', {embedded: true, readOnly: true}) | ||
}); | ||
@@ -49,6 +53,10 @@ | ||
likes : $b.hasMany('user'), | ||
views: $b.hasMany('user', {internal: true}), | ||
showTo : $b.hasMany('user', {readOnly: true}), | ||
permissions : $b.hasMany('permission', { | ||
map : {key : 'user', value : 'value'} | ||
map : {key : 'user', value : 'value'}, | ||
internal: true | ||
}) | ||
}); | ||
}); | ||
@@ -58,2 +66,15 @@ | ||
store = $b.Store.create(); | ||
emptyPostJSON = { | ||
comments: [], | ||
likes: [], | ||
views: [], | ||
permissions: {}, | ||
showTo: [] | ||
}; | ||
emptyCommentJSON = { | ||
likes: [], | ||
showTo: [], | ||
views: [] | ||
}; | ||
}); | ||
@@ -102,3 +123,3 @@ | ||
expect(post.serialize()).to.deep.equal(json); | ||
expect(post.serialize()).to.deep.equal($b.extend(emptyPostJSON, json)); | ||
}); | ||
@@ -109,3 +130,4 @@ | ||
var json, | ||
post; | ||
post, | ||
expectation; | ||
@@ -133,3 +155,8 @@ json = { | ||
expect(post.serialize()).to.deep.equal(json); | ||
expectation = $b.extend(emptyPostJSON, json); | ||
expectation.comments.forEach(function (v, k, a) { | ||
a[k] = $b.extend({}, emptyCommentJSON, v); | ||
}); | ||
expect(post.serialize()).to.deep.equal(expectation); | ||
}); | ||
@@ -178,3 +205,3 @@ | ||
expect(post.serialize()).to.deep.equal(json); | ||
expect(post.serialize()).to.deep.equal($b.extend(emptyPostJSON, json)); | ||
}); | ||
@@ -186,3 +213,4 @@ | ||
json, | ||
post; | ||
post, | ||
expectation; | ||
@@ -214,5 +242,69 @@ for (i = 1; i <= 5; i ++) { | ||
expect(post.serialize()).to.deep.equal(json); | ||
expectation = $b.extend(emptyPostJSON, json); | ||
expectation.comments.forEach(function (v, k, a) { | ||
a[k] = $b.extend({}, emptyCommentJSON, v); | ||
}); | ||
expect(post.serialize()).to.deep.equal(expectation); | ||
}); | ||
it('should properly deserialize and serialize complex relationships with filters.', function () { | ||
var i, | ||
json, | ||
post; | ||
for (i = 1; i <= 5; i ++) { | ||
store.add('user', User.create({id : i, name : 'User #' + i})); | ||
} | ||
json = { | ||
author : 1, | ||
content : 'post...', | ||
comments : [ | ||
{content : 'comment 1', author : 1, likes : [3, 2, 5]}, | ||
{content : 'comment 2', author : 2}, | ||
{content : 'comment 3', author : 3, likes : [2]}, | ||
{content : 'comment 4', author : 4, showTo: [{name: 'ralph'}]}, | ||
{content : 'comment 5', author : 5, likes : [1, 5], views: [1, 5]} | ||
], | ||
likes : [1, 3], | ||
views: [1, 5], | ||
showTo: [], | ||
permissions : { | ||
'user1' : 0, | ||
'user2' : 1, | ||
'user3' : 2, | ||
'user4' : 3 | ||
} | ||
}; | ||
Post.likes = $b.hasMany('user', {internal: true}); | ||
post = Post.create(); | ||
store.add('post', post); | ||
post.deserialize(json, false, function (meta) { | ||
return !meta.options.readOnly; | ||
}); | ||
post.comments.content[3].showTo.content.push(User.create({name: 'bart'})); | ||
expect(post.serialize(function (meta) { | ||
return !meta.options.internal; | ||
})).to.deep.equal({ | ||
author : 1, | ||
content : 'post...', | ||
comments : [ | ||
{content : 'comment 1', author : 1, likes : [3, 2, 5], showTo: []}, | ||
{content : 'comment 2', author : 2, likes : [], showTo: []}, | ||
{content : 'comment 3', author : 3, likes : [2], showTo: []}, | ||
{content : 'comment 4', author : 4, likes : [], showTo: [{name: 'bart'}]}, | ||
{content : 'comment 5', author : 5, likes : [1, 5], showTo: []} | ||
], | ||
likes : [1, 3], | ||
showTo: [] | ||
}); | ||
}); | ||
it('should properly revert hasManys.', function () { | ||
@@ -222,2 +314,3 @@ | ||
json, | ||
originalSerialized, | ||
post; | ||
@@ -248,7 +341,12 @@ | ||
expect(post.serialize()).to.not.deep.equal(json); | ||
originalSerialized = $b.extend(emptyPostJSON, json); | ||
originalSerialized.comments.forEach(function (v, k, a) { | ||
a[k] = $b.extend({}, emptyCommentJSON, v); | ||
}); | ||
expect(post.serialize()).to.not.deep.equal(originalSerialized); | ||
post.revert(); | ||
expect(post.serialize()).to.deep.equal(json); | ||
expect(post.serialize()).to.deep.equal(originalSerialized); | ||
}); | ||
}); |
@@ -79,2 +79,34 @@ describe('serialize', function () { | ||
}); | ||
it('should properly filter nested keys', function (done) { | ||
var json, | ||
Model, | ||
expected, | ||
instance; | ||
Model = $b.Model({ | ||
a : $b.attr({key : 'a.b.c.d', internal: true}) | ||
}); | ||
instance = Model.create(); | ||
instance.a = 'test'; | ||
expected = { | ||
a : { | ||
b : { | ||
c : { | ||
d : 'test' | ||
} | ||
} | ||
} | ||
}; | ||
json = instance.serialize(function (meta) { | ||
return !meta.options.internal; | ||
}); | ||
expect(json).to.deep.equal({}); | ||
done(); | ||
}); | ||
}); |
969056
160
20890
406