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

Is there a way to write plugins (custom methods) for this? #59

Closed
raniesantos opened this issue Mar 27, 2018 · 3 comments
Closed

Is there a way to write plugins (custom methods) for this? #59

raniesantos opened this issue Mar 27, 2018 · 3 comments

Comments

@raniesantos
Copy link

Is there a way to write plugins (custom methods) for this? The Marcus Westin version did but I'm trying to switch to your store package.

I have this code in my plugin for Marcus' package and I want to move it over.

window.store.addPlugin(() => {
    return {
        pull (_, key) {
            const value = this.get(key);
            this.remove(key);
            return value;
        },
        has (_, key) {
            return (typeof this.get(key) !== 'undefined');
        }
    };
});
@nbubna
Copy link
Owner

nbubna commented Mar 27, 2018

Yeah, there is store._.fn(name, fn), and there are plugins here you can use as examples: https://github.com/nbubna/store/tree/master/src

Adding a plugin function using the _.fn function, ensures that your plugin is available on all store interfaces, including those already created (e.g. store.session and any namespaces). That said, the functions in your plugin are already covered by this library:

Nonetheless, if you want the name "pull" or an "isUndef" function as plugins, i would write them something like this:

(function(_) {
  // create "pull" alias for remove
  _.fn('pull', function(key) {
     return this.remove(key);
  };
  // create fn to check for undefined values
  _.fn('isUndef', function(key) {
     return (typeof this.get(key) !== 'undefined');
  };
})(window.store._);

I'm going to leave this open, if you don't mind, as a reminder to add a little plugin documentation and update the doc for remove to point out that it returns any removed value.

@nbubna
Copy link
Owner

nbubna commented Mar 27, 2018

Alright, i updated the README to say that remove returns removed data and add a simple extension example. Thanks!

@nbubna nbubna closed this as completed Mar 27, 2018
@raniesantos
Copy link
Author

raniesantos commented Mar 27, 2018

Thanks for the info, you even went ahead and converted my code!

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