-
Notifications
You must be signed in to change notification settings - Fork 330
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
Nicer createActions #173
Comments
For better IDE support you can define your actions like that: var actions = { init: { startGame: Reflux.createAction(), signIn: Reflux.createAction() } } It's just a little bit more verbose ;) |
True, but it separates preEmit and shouldEmit. |
The idea has been put in PR #116 I'm open to the middleware approach, though might as well have a connect/express approach to it. Something like this: var myAction = Reflux.createAction();
myAction.use(function(args, next) {
if (args[0] === true) {
next();
}
});
myAction.use(function(args, next) {
console.log(args[1]);
});
// and myAction.use(fn, [fn, [...]]);
// to make the ordering apparent
myAction(false, 'dude');
myAction(true, 'dude where is my car?');
// Outputs: "dude where is my car?" That way you can reuse validation and transformations. |
I thought of middleware while composing this issue, I like middleware approach. But in the same time interface of middleware function should be neat. Personally I don't like
function(args, next) {
console.log(args[0]); // what does `args[0]` mean?
}
function(args, next) {
// to much to write
var someMessage = args[0];
console.log(someMessage);
} And that is why I am voting for natural function arguments.
In my opinion, we should think of utilising myAction.use(function(arg1, arg2) {
// Variant 1
if (args1 !== true) {
this.cancelAction(); // prevent all middlewares and do not emit action
}
// Variant 2
if (args1 === true) {
this.next(); // I do not like it personally, because .next() could be forgotten
}
// Variant 3 (jQuery-DOM-inspired)
if (args1 !== true) {
this.preventDefault(); // do not emit action
this.stopPropagation(); // skip all middlewares
// Helper for preventDefault+stopPropagation
return false;
}
}); Personally I like |
I believe that use next() as argument is better because connect/primus and other libraries use it in this way. And it is a clean way to async code. // or Reflux.ActionMethods.use(...) // for all actions created
// or Actions.use(...) for a group of actions
// or Actions.load.use(..) for a single action
myAction.use(function(packet, next) {
packet.arguments; //original arguments sent to this action, can be overwritten
this.actionName(); // or maybe packet.actionName()
next(false); // stop propagation
next(); // middleware ok, keep going
}); 1 - Use a object "packet" instead of arguments directly, in future packet can has more data/info. But, if you really dont like 'next', reflux can use middleware like that: // only one parameter, it is a sync middleware
// returning a false value (diff from undefined) it abort, otherwise it keep going
.use(function(packet) { ...});
// 2 arguments, it is a async code and need call next() or next(false)
.use(function(packet, next) { ...}); |
I noticed a few issues (imo) with actions.
In the one hand It gives a nice overview of all existing actions, but in the same time one should scroll back and forth to find action signature.
Most IDEs loose autocomplete action name as they expect explicitly defined methods or something that looks like
Object.create
User can access private
preEmit
andshouldEmit
methods as they are defined on actionIt is good to join
preEmit
andshouldEmit
in order to keep familiar pattern of DOM even handlersMy proposal is better to illustrate with a code:
cancelAction
works in the same way asDOMEvent.preventDefault
The text was updated successfully, but these errors were encountered: