From 0a2f2e4d6de3f660c97fd143c61ae3627c5a9ff5 Mon Sep 17 00:00:00 2001 From: Donovan Walker Date: Sun, 10 Sep 2017 18:11:27 -0500 Subject: [PATCH] Version bump. Add support for toJSON of null valued attributes --- log.md | 3 ++- package.json | 2 +- src/base.js | 9 ++++++--- test/base.model.js | 3 ++- test/base.model.jsonmap.from.js | 3 ++- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/log.md b/log.md index 2917cac..66149d8 100644 --- a/log.md +++ b/log.md @@ -1,7 +1,8 @@ ## Log: +###2017.09 +* .toJSON - added support for null value attributes. :( ###2017.04 * Full support for dotPath on collections. - ###2017.03 * More tests (now above 90%) * Added support for dotPath to Models. diff --git a/package.json b/package.json index 32e27c5..b83b408 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dw-backbone", - "version": "0.7.0", + "version": "0.7.1", "description": "", "main": "src/base.js", "scripts": { diff --git a/src/base.js b/src/base.js index b7f92db..4b441ad 100644 --- a/src/base.js +++ b/src/base.js @@ -581,11 +581,14 @@ _.each(keys, function(key) { if(typeof this.attributes[key] !== 'undefined') { rspAttrName = (map.attrs[key] && map.attrs[key].fieldName) ? map.attrs[key].fieldName : converter(key); - rsp[rspAttrName] = (typeof this.attributes[key].toJSON === 'function') ? this.attributes[key].toJSON(options, mode) : (typeof this.attributes[key] === 'object') ? converter(this.attributes[key]) : this.attributes[key]; + + rsp[rspAttrName] = (('object' !== typeof this.attributes[key]) || (null === this.attributes[key]))? this.attributes[key] : + ('function' === typeof this.attributes[key].toJSON) ? this.attributes[key].toJSON(options, mode) : converter(this.attributes[key]); + if (map.attrs[key] && map.attrs[key].fn) { - if (map.attrs[key].fn == 'stringify') { + if (map.attrs[key].fn === 'stringify') { rsp[rspAttrName] = JSON.stringify(rsp[rspAttrName]); - } else if (map.attrs[key].fn == 'parse') { + } else if (map.attrs[key].fn === 'parse') { rsp[rspAttrName] = JSON.parse(rsp[rspAttrName]); } else if (typeof map.attrs[key].fn === 'function') { rsp[rspAttrName] = map.attrs[key].fn.call(this, rsp[rspAttrName]); diff --git a/test/base.model.js b/test/base.model.js index fceffe7..867f2cc 100644 --- a/test/base.model.js +++ b/test/base.model.js @@ -268,7 +268,8 @@ describe('Base.Model', function() { c:'apple', object:{ 'pear':'fruit', - 'banana':'more fruit' + 'banana':'more fruit', + 'mango':null } }; model = new Model(attributes); diff --git a/test/base.model.jsonmap.from.js b/test/base.model.jsonmap.from.js index a1cdcc4..b4d7574 100644 --- a/test/base.model.jsonmap.from.js +++ b/test/base.model.jsonmap.from.js @@ -37,7 +37,8 @@ describe("Base.Model.jsonMap..from - transform input {...} on 'new Base.Mo c:'apple', objectOne:{ 'pear':'fruit', - 'banana':'more fruit' + 'banana':'more fruit', + 'mango':null }, object_two:{ 'pear':'glim',