goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 322 forks source link

How to add new actions in runtime correctly? #472

Closed aksonov closed 9 years ago

aksonov commented 9 years ago

I want to add 'syntax' sugar and generate new actions which uses existing ones for my react-native-router-flux i'm generating actions like Action.login(data) which just call Actions.push('login', data).

I'm just adding them using Actions[name]=function(data){...} and it works fine, but with minificator brokes it, here is issue for my component: https://github.com/aksonov/react-native-router-flux/issues/4

aksonov commented 9 years ago

Here is code, maybe i'm not working correctly with anonymous functions?

                if (!(RouterActions[name])) {
                    RouterActions[name] = function (data) {
                        if (typeof(data)!='object'){
                            data={data:data};
                        }
                        var args = {name: name, data:data};
                        RouterActions.push(args);
                    }
                }
mull commented 9 years ago

If it works before minifying it's likely you have strings/variable names that are being changed by the minification process. I would love to help out with this, would you be willing to set up something clonable that I could test?

aksonov commented 9 years ago

Yes, you are right, i'm using variable name for action name. I don't know how to make it correctly with minificator. Sure, you could check demo from https://github.com/aksonov/react-native-router-flux, it will be broken after minification, but works without minification.

mull commented 9 years ago

Hey @aksonov I haven't worked with react native before, but I sent a PR (https://github.com/aksonov/react-native-router-flux/pull/5) that might be a quick fix for your issues. Could you check please? If not I'd still like to help but I need to figure out exactly how to get the example launched.

aksonov commented 9 years ago

I've posted screenshot with error. To launch example, just open Example/ folder, run 'npm install' and launch Example.xcodeproj

mull commented 9 years ago

@goatslacker / @taion since I'm new here I would like to hear advise on this. What @aksonov is building is pretty cool but the way it's built right now assumes that the value returned from alt.createActions is mutable. He basically has a DSL similar to react-router:

// This is not word for word correct
<Router>
  <Schema name="login" handler={SomeComponent} transition=""/>
</router>

The DSL specifies views/routes for a react native app, and let's you specify what kind of transition to use when navigating to it. It's very cool!

From each <Schema/> he gets a name for the action say "login". He then wishes to mutate an action to add login to it which in turn calls .push("login"). I don't think mutating actions is anything alt would like to provide, as it's prone to some very dangerous coding. I'm taking the liberty to close this issue.

goatslacker commented 9 years ago

I don't quite get it, are these supposed to be actions?

Doing RouterActions.foo = function () { } won't make an action RouterActions.foo

aksonov commented 9 years ago

Yes, i want to make it as real actions and I suppose that my current way is 'hacky' and not good (but it works, however only without obfuscation), but don't know correct way to do it - for given variable name, create action with given name (which will pass that name to some generic action)

goatslacker commented 9 years ago

const MyAction = alt.createAction('nameOfTheAction', function () {
  callAnotherAction('nameOfTheAction')
})
aksonov commented 9 years ago

Thanks, is there anyway to add that action to existing set of actions?

Pavel.

5 сент. 2015, в 21:36, Josh Perez notifications@github.com написал(а):

const MyAction = alt.createAction('nameOfTheAction', function () {' callAnotherAction('nameOfTheAction') }) — Reply to this email directly or view it on GitHub.

goatslacker commented 9 years ago
ExistingActions.nameOfTheAction = alt.createAction('nameOfTheAction', function () {
  callAnotherAction('nameOfTheAction')
})