diff --git a/store.js b/store.js index 63a99cca05..9f0661811b 100644 --- a/store.js +++ b/store.js @@ -111,10 +111,13 @@ assign(Store.prototype, { onchange: function(callback) { this._changeHandlers.push(callback); + + var store = this; + return { cancel: function() { - var index = this._changeHandlers.indexOf(callback); - if (~index) this._changeHandlers.splice(index, 1); + var index = store._changeHandlers.indexOf(callback); + if (~index) store._changeHandlers.splice(index, 1); } }; }, @@ -163,4 +166,4 @@ assign(Store.prototype, { } }); -export { Store }; \ No newline at end of file +export { Store }; diff --git a/test/store/index.js b/test/store/index.js index e3c024e4d5..fd84322d35 100644 --- a/test/store/index.js +++ b/test/store/index.js @@ -113,6 +113,15 @@ describe('store', () => { }); }); + it('allows user to cancel state change callback', () => { + const store = new Store(); + const handler = store.onchange(() => {}); + + assert.doesNotThrow(() => { + handler.cancel(); + }, TypeError, 'this._changeHandlers is undefined'); + }); + describe('computed', () => { it('computes a property based on data', () => { const store = new Store({