From 5076ee0b88fe8d8d6498692e2ce0c4ba7294006c Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Wed, 18 Nov 2015 15:07:35 -0800 Subject: [PATCH 1/2] Avoid throwing with invalid keys/paths. Fixes #3018. --- src/lib/collection.html | 36 ++++++++++++++++++----------------- src/standard/notify-path.html | 4 +++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/lib/collection.html b/src/lib/collection.html index 71cd691336..997b3a5f3a 100644 --- a/src/lib/collection.html +++ b/src/lib/collection.html @@ -50,9 +50,10 @@ }, removeKey: function(key) { - key = this._parseKey(key); - this._removeFromMap(this.store[key]); - delete this.store[key]; + if (key = this._parseKey(key)) { + this._removeFromMap(this.store[key]); + delete this.store[key]; + } }, _removeFromMap: function(item) { @@ -88,29 +89,30 @@ }, _parseKey: function(key) { - if (key[0] == '#') { + if (key && key[0] == '#') { return key.slice(1); } - throw new Error('unexpected key ' + key); }, setItem: function(key, item) { - key = this._parseKey(key); - var old = this.store[key]; - if (old) { - this._removeFromMap(old); - } - if (item && typeof item == 'object') { - this.omap.set(item, key); - } else { - this.pmap[item] = key; + if (key = this._parseKey(key)) { + var old = this.store[key]; + if (old) { + this._removeFromMap(old); + } + if (item && typeof item == 'object') { + this.omap.set(item, key); + } else { + this.pmap[item] = key; + } + this.store[key] = item; } - this.store[key] = item; }, getItem: function(key) { - key = this._parseKey(key); - return this.store[key]; + if (key = this._parseKey(key)) { + return this.store[key]; + } }, getItems: function() { diff --git a/src/standard/notify-path.html b/src/standard/notify-path.html index 927f8403d4..328a6cbba9 100644 --- a/src/standard/notify-path.html +++ b/src/standard/notify-path.html @@ -85,7 +85,9 @@ var info = {}; this._get(path, this, info); // Notify change to key-based path - this._notifyPath(info.path, value, fromAbove); + if (info.path) { + this._notifyPath(info.path, value, fromAbove); + } }, // Note: this implemetation only accepts key-based array paths From 108b7f98e49cad55bcb76a9de6711a975adda746 Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Thu, 19 Nov 2015 12:07:13 -0800 Subject: [PATCH 2/2] Add a couple of tests. --- test/unit/notify-path.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/unit/notify-path.html b/test/unit/notify-path.html index 11d42f22ca..7cc82c414d 100644 --- a/test/unit/notify-path.html +++ b/test/unit/notify-path.html @@ -856,6 +856,18 @@ assert.equal(el.get('nested.again.again.wayOverThere'), 99); }); + test('notifyPath calls observer', function() { + el.a = {b: {c: true}}; + sinon.spy(el, 'aChanged'); + el.notifyPath('a.b.c', el.a.b.c); + assert.isTrue(el.aChanged.calledOnce); + assert.equal(el.get('a.b.c'), true); + }); + + test('notifyPath a non-extistant does nothing', function() { + el.notifyPath('does.not.exist', true); + }); + test('get array', function() { el.arrayChanged = function() {}; el.array = [1, 2, 3];