This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Flux Architecture: Application Actions

3:25 React lesson by

In this lesson we'll build our actions which will be sent to our dispatcher.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In this lesson we'll build our actions which will be sent to our dispatcher.

Avatar
mahesh

What do .bind(null,"xxxxxxx') do ? . Why do we need to bind it ?

In reply to egghead.io
Avatar
Joel

What do .bind(null,"xxxxxxx') do ? . Why do we need to bind it ?

You can check out this lesson for some details on binding functions in JavaScript.

In reply to mahesh
Avatar
Jacob

This got me too. Here is a good link: http://stackoverflow.com/questions/27654149/function-prototype-bind-with-null-as-argument

Basically, it makes a new function in the global scope (hence the null) and the "xxxxxx" would be the first parameter. It's syntactic sugar.

In reply to mahesh
Avatar
Keir Beckett

What does the 'action' parameter get passed in the 'flux.dispatcher' function?

Avatar
Joseph

It represents a typo. :)
dispatcher takes a single argument called payload. While having action and actionType (or other) fields in that payload can be useful in a larger application it is not used in this series. Expect an update to the dispatcher lesson to resolve any confusion.

In reply to Keir Beckett
Avatar
Homan

In app-actions why is dispatch function called with curly braces?

addItem( item ){
dispatch({
actionType: AppConstants.ADDITEM, item
})
},
why isn't it simply called with 2 arguments? If using named parameters and passing an object, why doesn't the second parameter have a key as well? like: {actionType: AppConstants.ADD
ITEM, item: item } ?

Avatar
Shawn

Following with what Joe mentioned, as of this reply there is a typo in the file app-dispatcher.js. The dispatch() function definition is meant to accept one object (named payload) as an argument (Facebook Flux API guide). This payload object (for this application so far) has 2 properties: actionType and item. In app-actions.js, the curly braces surrounding actionType and item indicate that dispatch() is being passed an object literal (for the payload). The 2nd property "item", is using es6 object property shorthand notation.

To clear this up, go to app-dispatcher.js and replace the exported dispatch function with:
export function dispatch( payload ) {
console.log(payload);
flux.dispatch( payload );
}

In reply to Homan
Avatar
Daniel Cortes

So why are you not getting an Uncaught ReferenceError: item is not defined?

In reply to egghead.io
Avatar
rajeshkw

import Dispatcher from 'flux';
const flux = new Dispatcher(); //constructor error for flux 3.0

i had to use below instead..
import Flux from 'flux';
export default new Flux.Dispatcher;

The first step in creating actions for our Flux application is to create a list of those actions. Here in the constants directory I'm going to create a new file. I'm just going to call that app-constants.js. In here we're just going to export an object that lists each of those actions.

The first one's going to be add-item and its value is going to be add item. We're just going to continue on and what we're going to be creating here is a shopping cart application, so we'll just have actions that relate to adding and removing items from a shopping cart. We'll have things like increase-item and decrease-item.

We now have a list of all the actions that are available on this application. This could obviously get much more complex, but again for our application we've just got a simple list of actions.

Now we're going to move on to creating the actual actions here in the actions directory. We're going to create a new file called app-actions.js. Here we're going to import our app constants from Constants, app-constants. We're also going to import dispatch and register from our dispatcher, app-dispatcher.

We're going to get a little more room on the screen here. We're going to export a default object and we're going to have a handful of items here. They're all going to follow the exact same pattern, so add-item while taking an item. It'll call dispatch. We're going to pass our entire payload here in the first argument so it's going to have an action type. In this case it's going to be app-constants.add-item. It's also going to have the key of item, which matches the item that was passed in.

We're just going to copy these guys down here. 2, 3, 4. This one will be remove, with a key of remove. This one will be increase with a key of increase-item. Then finally decrease with a key of decrease-item. Our actions are all now set up and we can't really do anything with them until we have a store.

However, if we wanted to get a look at how all this is working we could jump over to our dispatcher and right here, the logout, the action type. We'll jump over to our app component and we'll import app-actions from actions, app-actions. Then here in the on-click, let's just break this out into a new line. On-click equals app-actions.add-item, which is one of the items we just created. We'll go ahead and bind that. Null is OK in this case and we'll go ahead and pass in...this is the item.

We'll save that and over here in the browser we should be able to bring up the dev tools and when we click on this guy we can see our action being passed into our dispatcher.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?