Skip to content

Commit

Permalink
Merge f610b87 into b7c60c7
Browse files Browse the repository at this point in the history
  • Loading branch information
pgilad committed Oct 28, 2015
2 parents b7c60c7 + f610b87 commit 623d027
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ampersand-state.js
Expand Up @@ -376,7 +376,7 @@ assign(Base.prototype, Events, {
},

getAttributes: function (options, raw) {
assign({
options = assign({
session: false,
props: false,
derived: false
Expand All @@ -386,7 +386,8 @@ assign(Base.prototype, Events, {
for (item in this._definition) {
def = this._definition[item];
if ((options.session && def.session) || (options.props && !def.session)) {
val = (raw) ? this._values[item] : this[item];
val = raw ? this._values[item] : this[item];
if (raw && val && isFunction(val.serialize)) val = val.serialize();
if (typeof val === 'undefined') val = result(def, 'default');
if (typeof val !== 'undefined') res[item] = val;
}
Expand Down
69 changes: 69 additions & 0 deletions test/full.js
Expand Up @@ -1745,3 +1745,72 @@ test('collision in model extend - issue #144', function(t) {
}, Error, 'throws a collision error on extending a prop');
t.end();
});

test('toJSON should serialize state props - issue #197', function(t) {
var Person = State.extend({
props: {
child: {
type: 'state'
}
}
});

var Child = State.extend({
props: {
name: {
type: 'string'
}
}
});

var father = new Person();
var child = new Child({ name: 'john' });
father.child = child;
t.deepEqual(father.toJSON(), { child: { name: 'john' }}, 'should serialize existing state props');

var mother = new Person();
var child2 = new Child();
mother.child = child2;
t.deepEqual(mother.toJSON(), { child: {}}, 'should serialize non-existent state props');

t.end();
});

test('toJSON should serialize customType props - issue #197', function(t) {
function CustomType(props) {
this.props = props;
this.serialize = function() {
return this.props;
};
}
var Person = State.extend({
dataTypes: {
customType: {
set: function(newVal) {
return {
val: newVal,
type: 'customType'
};
},
compare: function(currentVal, newVal, attributeName) {
return currentVal === newVal;
}
},
compare : function(currentVal, newVal, attributeName){
return currentVal.equals(newVal);
}
},
props: {
child: {
type: 'customType'
}
}
});

var father = new Person();
var child = new CustomType({ name: 'john'});
father.child = child;
t.deepEqual(father.toJSON(), { child: { name: 'john' }}, 'should serialize existing state props');

t.end();
});

0 comments on commit 623d027

Please sign in to comment.