Add key.deleteScope(scope) to remove all handlers #31

Merged
merged 2 commits into from Mar 21, 2012

Conversation

Projects
None yet
3 participants
Contributor

agnoster commented Nov 3, 2011

Removes all handlers associated with a given scope, allowing throw-away and re-usable scopes. Includes test. Adds 1 function, 8 lines of functional code, and 117 bytes to the minified version.

I took the liberty of adding a simple test for scope while I was at it.

I love how keymaster is so simple and small, I hope this addition does not constitute undue bloat.

If you're curious why I added this function: on a single-site app I'm building we want to be able to be able to switch to different "apps" with their own shortcuts. However, when we navigate away from the app it is destroyed, and a new one is created when we navigate back. Allowing us to destroy the scope of all shortcuts the app registered means we don't need to worry about callbacks to a dead app being fired (and of course the app not getting garbage-collected because the closures hold references to it). Of course, if there's another, better way to do this, I'd be happy to hear that too. Now, we can just do:

// on app open
key('n', this.appName, this.createNewItem);
// ... and other shortcuts...
key.setScope(this.appName);

// on app close
key.setScope('all');
key.deleteScope(this.appName);

agnoster added some commits Nov 3, 2011

Add key.deleteScope(scope) to remove all handlers
Removes all handlers associated with a given scope, allowing throw-away and re-usable scopes. Includes test. Adds 1 function, 8 lines of functional code, and 117 bytes to the minified version.

Baggz commented Dec 26, 2011

+1

madrobby added a commit that referenced this pull request Mar 21, 2012

Merge pull request #31 from agnoster/keymaster
---

Removes all handlers associated with a given scope, allowing throw-away and re-usable scopes. Includes test. Adds 1 function, 8 lines of functional code, and 117 bytes to the minified version.

I took the liberty of adding a simple test for scope while I was at it.

I love how keymaster is so simple and small, I hope this addition does not constitute undue bloat.

If youre curious why I added this function: on a single-site app Im building we want to be able to be able to switch to different "apps" with their own shortcuts. However, when we navigate away from the app it is destroyed, and a new one is created when we navigate back. Allowing us to destroy the scope of all shortcuts the app registered means we dont need to worry about callbacks to a dead app being fired (and of course the app not getting garbage-collected because the closures hold references to it). Of course, if theres another, better way to do this, Id be happy to hear that too. Now, we can just do:

    // on app open
    key(n, this.appName, this.createNewItem);
    // ... and other shortcuts...
    key.setScope(this.appName);

    // on app close
    key.setScope(all);
    key.deleteScope(this.appName);

Conflicts:
	keymaster.js
	keymaster.min.js

@madrobby madrobby merged commit 9c60b2c into madrobby:master Mar 21, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment