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

this.emit('change') vs this.emitChange() #91

Open
macalinao opened this issue Jan 9, 2015 · 3 comments
Open

this.emit('change') vs this.emitChange() #91

macalinao opened this issue Jan 9, 2015 · 3 comments

Comments

@macalinao
Copy link

It seems odd to have this one event not use a constant when every other emitted event should be using constants. I think that there should be this method in stores to prevent typos.

@BinaryMuse
Copy link
Owner

I've been thinking about this quite a bit since #58, but I'm hesitant to make it part of the base store API.

It's likely that an event named "change" is probably the most common event used, especially since StoreWatchMixin uses it, but it's certainly far from the only one I've used (an app I have in production emits several different events), and users are free to use whatever event name(s) they want.

I've considered a method called changed() that does the same thing that makes the whole system feel a bit more cohesive (e.g. you don't really have to know that you're emitting an event, as the changed method and the StoreWatchMixin take care of it for you), but I'm leery of "too much magic"—my goal is convenience, but without hiding the underlying power that the system is based on (e.g. the fact that you have an entire EventEmitter at your disposal).

The typo argument is valid, but I think a bit weak, as one could easily define a constant somewhere and use it for event emissions:

var CHANGE = "change";
// ...
this.emit(CHANGE);

I'm also fond of the idea of mixins (see #37), and while the proposed PR adds a considerable amount of complexity to Fluxxor's stores, version 2 of the Fluxxor API will have the ability to use completely custom stores if you so wish.

In the meantime, if there are methods you want to use in multiple stores, you can use Object.assign (or the object-assign module or extend from Underscore/assign from Lo-Dash) to mix them in to a spec:

// store_mixin.js
module.exports = {
  emitChange: function() { this.emit("change"); },
  // maybe other methods here
};

// my_store.js
var assign = Object.assign || require("object-assign"),
    storeMixin = require("./store_mixin");

module.exports = Fluxxor.createStore(assign({}, storeMixin, {
  handleEvent: function() {
    // ...
    this.emitChange();
  }
}));

I'm going to leave this open for now to foster discussion.

@davejacobs
Copy link

One thing that I really like about this.emit("change") is that it encourages you to think about whether custom events make sense for your particular app. Without the explicit string, it might be easy to assume that custom events are bad practice.

@macalinao
Copy link
Author

I mean, the change event is enforced by StoreWatchMixin. Custom events
are definitely fine, but an event that's enforced by the library imo should
have a dedicated function (or constant) for this.

On Sun, Jan 11, 2015 at 2:50 PM, David Jacobs notifications@github.com
wrote:

One thing that I really like about this.emit("change") is that it
encourages you to think about whether custom events make sense for your
particular app. Without the explicit string, it might be easy to assume
that custom events are bad practice.


Reply to this email directly or view it on GitHub
#91 (comment).

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

3 participants