Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add store.one() method to store.on.js #9

Closed
aensley opened this issue Jun 28, 2013 · 2 comments
Closed

Add store.one() method to store.on.js #9

aensley opened this issue Jun 28, 2013 · 2 comments

Comments

@aensley
Copy link

aensley commented Jun 28, 2013

I'm finding myself needing to attach an event handler to a key's change event only once. I'm used to jQuery's .one() method and am emulating it right now with:

function myStoreOne(key, fn)
{
    var handler = function(e){
        store.off(key, handler); // unbind to prevent future events.
        return fn.call(this, e);
    };
    store.on(key, handler); // bind to the event.
}

Would be nice to be able to just call store.one(key, fn) instead.

@nbubna
Copy link
Owner

nbubna commented Jun 28, 2013

Store is extensible; if you do it like this, you make it available for all namespaces and storage areas:

store._.fn('one', function(key, fn) {
    if (!fn) { fn = key; key = ''; }// no key === all keys
    var s = this, _fn;
    return s.on(key, _fn = function(e) {
        s.off(key, _fn);
        return fn.apply(this, arguments);
    };
}

As for adding it to store.on, i'm not digging the store.one() naming. If you aren't friendly with jQuery.one, then it doesn't look like an event method. store.onOne is clearer, but admittedly less graceful. Maybe store.once? Argh. I'd like to believe there's always a perfect name, but alas... Failing that, what do you prefer?

@aensley
Copy link
Author

aensley commented Jul 1, 2013

I like store.one(). I know not everyone uses jQuery, but it is pretty ubiquitous.

I also like store.once(). I think that's a good compromise, and it clearly indicates what it does. I'm not sure why jQuery didn't go that route. It's much clearer.

@nbubna nbubna closed this as completed in 09f7acb Aug 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants