Skip to content

Commit

Permalink
Set {} results in an empty model instead of undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
tanqhnguyen committed Apr 15, 2014
1 parent f73d40c commit ca65d8b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
14 changes: 10 additions & 4 deletions build/backbone.supermodel.amd.js
Expand Up @@ -154,11 +154,17 @@
}

var finalPath = path[lastKeyIndex];

if (!_.isArray(value) && _.isObject(value) && isSimpleObject(value)) {
for (var j in value) {
var newPath = finalPath + '.' + j;
// let _nestedSet do its things
obj._nestedSet(newPath, value[j], options);
// special case when the object value is empty, just set it to an empty model
if (_.size(value) === 0) {
obj.attributes[finalPath] = new Model();
} else {
for (var j in value) {
var newPath = finalPath + '.' + j;
// let _nestedSet do its things
obj._nestedSet(newPath, value[j], options);
}
}
} else {
if (this._valueForCollection(value)) {
Expand Down
4 changes: 2 additions & 2 deletions build/backbone.supermodel.amd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions build/backbone.supermodel.js
Expand Up @@ -144,11 +144,17 @@ Backbone.SuperModel = (function(_, Backbone){
}

var finalPath = path[lastKeyIndex];

if (!_.isArray(value) && _.isObject(value) && isSimpleObject(value)) {
for (var j in value) {
var newPath = finalPath + '.' + j;
// let _nestedSet do its things
obj._nestedSet(newPath, value[j], options);
// special case when the object value is empty, just set it to an empty model
if (_.size(value) === 0) {
obj.attributes[finalPath] = new Model();
} else {
for (var j in value) {
var newPath = finalPath + '.' + j;
// let _nestedSet do its things
obj._nestedSet(newPath, value[j], options);
}
}
} else {
if (this._valueForCollection(value)) {
Expand Down
4 changes: 2 additions & 2 deletions build/backbone.supermodel.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions src/backbone.supermodel.js
Expand Up @@ -144,11 +144,17 @@ Backbone.SuperModel = (function(_, Backbone){
}

var finalPath = path[lastKeyIndex];

if (!_.isArray(value) && _.isObject(value) && isSimpleObject(value)) {
for (var j in value) {
var newPath = finalPath + '.' + j;
// let _nestedSet do its things
obj._nestedSet(newPath, value[j], options);
// special case when the object value is empty, just set it to an empty model
if (_.size(value) === 0) {
obj.attributes[finalPath] = new Model();
} else {
for (var j in value) {
var newPath = finalPath + '.' + j;
// let _nestedSet do its things
obj._nestedSet(newPath, value[j], options);
}
}
} else {
if (this._valueForCollection(value)) {
Expand Down
6 changes: 6 additions & 0 deletions tests.js
Expand Up @@ -135,6 +135,12 @@ describe('Backbone.SuperModel', function(){
should(this.zoo.get('location.address')).be.eql('address');
});

it('sets {}', function(){
this.zoo.set('a', {});

should(this.zoo.get('a')).be.an.instanceOf(SuperModel);
});

it('sets array', function(){
var locations = [
{
Expand Down

0 comments on commit ca65d8b

Please sign in to comment.