can-globals
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -5,6 +5,2 @@ 'use strict'; | ||
function errorMessage(key){ | ||
return key + ' is not defined!'; | ||
} | ||
function dispatch(key, value) { | ||
@@ -104,4 +100,2 @@ // jshint -W040 | ||
* | ||
* Getting a key which was not previously defined will result in an error. | ||
* | ||
* ```javascript | ||
@@ -134,9 +128,5 @@ * globals.define('foo', 'bar'); | ||
} | ||
throw errorMessage(key); | ||
}; | ||
Globals.prototype.makeExport = function(key) { | ||
if (key !== '' && !this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
return function(value) { | ||
@@ -166,3 +156,2 @@ if (arguments.length === 0) { | ||
* | ||
* Removing an observer for a key which was not previously defined will result in an error. | ||
* | ||
@@ -189,11 +178,10 @@ * ```javascript | ||
Globals.prototype.offKeyValue = function(key, handler) { | ||
if (!this.properties[key]) { | ||
throw errorMessage(key); | ||
if (this.properties[key]) { | ||
var handlers = this.eventHandlers[key]; | ||
if (handlers) { | ||
var i = handlers.indexOf(handler); | ||
handlers.splice(i, 1); | ||
} | ||
} | ||
var handlers = this.eventHandlers[key]; | ||
if (handlers) { | ||
var i = handlers.indexOf(handler); | ||
handlers.splice(i, 1); | ||
return this; | ||
} | ||
return this; | ||
}; | ||
@@ -211,4 +199,2 @@ | ||
* | ||
* Observing a key which was not previously defined will result in an error. Will not trigger for changes to the return value of lazy defaults. | ||
* | ||
* ```javascript | ||
@@ -232,9 +218,8 @@ * globals.define('foo', 'bar'); | ||
Globals.prototype.onKeyValue = function(key, handler) { | ||
if (!this.properties[key]) { | ||
throw errorMessage(key); | ||
if (this.properties[key]) { | ||
if (!this.eventHandlers[key]) { | ||
this.eventHandlers[key] = []; | ||
} | ||
this.eventHandlers[key].push(handler); | ||
} | ||
if (!this.eventHandlers[key]) { | ||
this.eventHandlers[key] = []; | ||
} | ||
this.eventHandlers[key].push(handler); | ||
return this; | ||
@@ -253,4 +238,2 @@ }; | ||
* | ||
* Deleting a key which was not previously defined will result in an error. | ||
* | ||
* ```javascript | ||
@@ -270,11 +253,9 @@ * globals.define('global', window); | ||
Globals.prototype.deleteKeyValue = function(key) { | ||
if (!this.properties[key]) { | ||
throw errorMessage(key); | ||
} | ||
var property = this.properties[key]; | ||
if (property !== undefined) { | ||
property.value = property.default; | ||
property.cachedValue = undefined; | ||
dispatch.call(this, key, property.value); | ||
return this; | ||
} | ||
return this; | ||
}; | ||
@@ -298,3 +279,3 @@ | ||
* | ||
* Setting a key which was not previously defined will result in an error. | ||
* Setting a key which was not previously defined will call `define` with the key and value. | ||
* | ||
@@ -312,6 +293,7 @@ * @param {String} key | ||
if (!this.properties[key]) { | ||
throw errorMessage(key); | ||
return this.define(key, value); | ||
} | ||
var property = this.properties[key]; | ||
property.value = value; | ||
property.cachedValue = undefined; | ||
dispatch.call(this, key, value); | ||
@@ -321,2 +303,31 @@ return this; | ||
/** | ||
* @function reset | ||
* @parent can-globals/methods | ||
* | ||
* Reset all keys to their default value and clear their caches. | ||
* | ||
* @signature `globals.setKeyValue(key, value)` | ||
* | ||
* ```javascript | ||
* globals.define('foo', 'bar'); | ||
* globals.setKeyValue('foo', 'baz'); | ||
* globals.getKeyValue('foo'); //-> 'baz' | ||
* globals.reset(); | ||
* globals.getKeyValue('foo'); //-> 'bar' | ||
* ``` | ||
* | ||
* @return {can-globals} | ||
* Returns the instance of `can-globals` for chaining. | ||
*/ | ||
Globals.prototype.reset = function(){ | ||
for(var key in this.properties){ | ||
if(this.properties.hasOwnProperty(key)){ | ||
this.properties[key].cachedValue = undefined; | ||
dispatch.call(this, key, this.getKeyValue(key)); | ||
} | ||
} | ||
return this; | ||
}; | ||
canReflect.assignSymbols(Globals.prototype, { | ||
@@ -323,0 +334,0 @@ 'can.getKeyValue': Globals.prototype.getKeyValue, |
@@ -25,60 +25,36 @@ 'use strict'; | ||
QUnit.test('get undefined property', function() { | ||
QUnit.test('getKeyValue of undefined property', function() { | ||
globals = new Globals(); | ||
try{ | ||
globals.getKeyValue('test'); | ||
}catch(e){ | ||
return ok(true); | ||
} | ||
ok(false); | ||
globals.getKeyValue('test'); | ||
ok(true); | ||
}); | ||
QUnit.test('set undefined property', function() { | ||
QUnit.test('setKeyValue of undefined property', function() { | ||
globals = new Globals(); | ||
try{ | ||
globals.setKeyValue('test'); | ||
}catch(e){ | ||
return ok(true); | ||
} | ||
ok(false); | ||
globals.setKeyValue('foo', 'bar'); | ||
equal(globals.getKeyValue('foo'), 'bar'); | ||
}); | ||
QUnit.test('reset undefined property', function() { | ||
QUnit.test('deleteKeyValue of undefined property', function() { | ||
globals = new Globals(); | ||
try{ | ||
globals.deleteKeyValue('test'); | ||
}catch(e){ | ||
return ok(true); | ||
} | ||
ok(false); | ||
globals.deleteKeyValue('test'); | ||
ok(true); | ||
}); | ||
QUnit.test('on undefined property', function() { | ||
QUnit.test('onKeyValue of undefined property', function() { | ||
globals = new Globals(); | ||
try{ | ||
globals.onKeyValue('test', function() {}); | ||
}catch(e){ | ||
return ok(true); | ||
} | ||
ok(false); | ||
globals.onKeyValue('test', function() {}); | ||
ok(true); | ||
}); | ||
QUnit.test('off undefined property', function() { | ||
QUnit.test('offKeyValue of undefined property', function() { | ||
globals = new Globals(); | ||
try{ | ||
globals.offKeyValue('test', function() {}); | ||
}catch(e){ | ||
return ok(true); | ||
} | ||
ok(false); | ||
globals.offKeyValue('test', function() {}); | ||
ok(true); | ||
}); | ||
QUnit.test('makeExport undefined property', function() { | ||
QUnit.test('makeExport of undefined property', function() { | ||
globals = new Globals(); | ||
try{ | ||
globals.makeExport('test'); | ||
}catch(e){ | ||
return ok(true); | ||
} | ||
ok(false); | ||
globals.makeExport('test'); | ||
ok(true); | ||
}); | ||
@@ -116,3 +92,3 @@ | ||
QUnit.test('set existing property to string', function() { | ||
QUnit.test('setKeyValue of existing property to string', function() { | ||
globals = new Globals(); | ||
@@ -124,3 +100,3 @@ globals.define('test', 'default'); | ||
QUnit.test('set existing property to undefined', function() { | ||
QUnit.test('setKeyValue of existing property to undefined', function() { | ||
globals = new Globals(); | ||
@@ -132,3 +108,3 @@ globals.define('test', 'default'); | ||
QUnit.test('set existing property to a function', function() { | ||
QUnit.test('setKeyValue of existing property to a function', function() { | ||
globals = new Globals(); | ||
@@ -142,4 +118,17 @@ globals.define('test', 'default'); | ||
QUnit.test('reset property to default', function() { | ||
QUnit.test('setKeyValue on an existing property should reset cache', function(){ | ||
var globals = new Globals(); | ||
var bar = sinon.spy(function(){ | ||
return 'bar'; | ||
}); | ||
globals.define('foo', bar); | ||
globals.getKeyValue('foo'); | ||
globals.setKeyValue('foo', function(){ | ||
return 'baz'; | ||
}); | ||
equal(globals.getKeyValue('foo'), 'baz'); | ||
}); | ||
QUnit.test('deleteKeyValue to reset property to default', function() { | ||
var globals = new Globals(); | ||
globals.define('test', 'default'); | ||
@@ -151,2 +140,17 @@ globals.setKeyValue('test', 'updated'); | ||
QUnit.test('deleteKeyValue should clear cache', function() { | ||
var globals = new Globals(); | ||
var bar = sinon.spy(function(){ | ||
return 'bar'; | ||
}); | ||
globals.define('foo', bar); | ||
globals.getKeyValue('foo'); | ||
globals.setKeyValue('foo', function(){ | ||
return 'baz'; | ||
}); | ||
globals.deleteKeyValue('foo'); | ||
globals.getKeyValue('foo'); | ||
equal(bar.calledTwice, true); | ||
}); | ||
QUnit.test('listen for key change', function() { | ||
@@ -178,3 +182,3 @@ var globals = new Globals(); | ||
QUnit.test('make export of key', function() { | ||
QUnit.test('makeExport of key', function() { | ||
var globals = new Globals(); | ||
@@ -200,1 +204,39 @@ globals.define('foo', 'bar'); | ||
}); | ||
QUnit.test('reset all keys', function(){ | ||
var globals = new Globals(); | ||
var bar = sinon.spy(function(){ | ||
return 'bar'; | ||
}); | ||
var qux = sinon.spy(function(){ | ||
return 'qux'; | ||
}); | ||
globals.define('foo', bar); | ||
globals.define('baz', qux); | ||
loop(function(){ | ||
globals.getKeyValue('foo'); | ||
globals.getKeyValue('baz'); | ||
}, 5); | ||
globals.reset(); | ||
loop(function(){ | ||
globals.getKeyValue('foo'); | ||
globals.getKeyValue('baz'); | ||
}, 5); | ||
equal(bar.calledTwice, true); | ||
equal(qux.calledTwice, true); | ||
}); | ||
QUnit.test('reset triggers events', function(){ | ||
var globals = new Globals(); | ||
var fooHandler = sinon.spy(); | ||
var barHandler = sinon.spy(); | ||
globals.define('foo', true); | ||
globals.define('bar', true); | ||
globals.setKeyValue('foo', false); | ||
globals.setKeyValue('bar', false); | ||
globals.onKeyValue('foo', fooHandler); | ||
globals.onKeyValue('bar', barHandler); | ||
globals.reset(); | ||
equal(fooHandler.called, true); | ||
equal(barHandler.called, true); | ||
}); |
{ | ||
"name": "can-globals", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "This module provides a dependency injection container. Modules may define a key and specify a default value (which can be static, cached lazy, or dynamic lazy), but other code can set and reset the value as needed. There is also an event system, for alerting on value changes, both specific to a key and for any key.", | ||
@@ -5,0 +5,0 @@ "main": "can-globals.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28049
753