This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Reflux - Using Any Library for Model Changes

2:30 React lesson by

Reflux actions and stores can be used with any library. In this video, John shows how to listen to store changes without using React.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Reflux actions and stores can be used with any library. In this video, John shows how to listen to store changes without using React.

Avatar
Adam

so, to be clear, the model is the store?

You can use reflux in any library, it doesn't have to be a react, so let's see if we can do that. We'll add an action with var action, and create a reflux action. Now we'll create a reflux store with var store, reflux create store. Then in our init function, we can this.listento our first action we created.

So, this.action here is that action that we already created, and whenever we invoke it, we'll say this.onAction, and set up that on action handler here. We'll just log out onAction. So, now when we invoke this action, just with action and run it, you can see in the console, it logged out onAction.

So we invoke the action, and the action is listened to at on action, and then the console logged out onAction. Now, typically you would listen to stored changes in a react component, but you can just say store.listen, then pass in a function that will be called whenever that store is updated, a change is triggered. So, a log out store updated.

So, instead of logging something out onNonaction, we just say this.trigger, and then run this. You can see it called store.updated in the console. So, this action was handled in onAction, which then triggers an update on the model, which is listened to in our function, and we say store updated.

Now, if we want to pass some data along in our action, we'll just go into our action where it's invoked. We'll just add an object with a property of message that says, "Live long and prosper." So now, when we handle this action in onAction, we'll just call that object data, and in this.trigger we'll pass through the data.

Now when we handle it, we'll pass through the data, so in the console, we have an object with a message of, "Live long and prosper." That message is coming through the action, then handled in onAction. The data is being passed along to trigger, the store listens and handles that data, and logs it out for us.

From here on out, whatever framework you're using, it's just simply a matter of using store.listen. In that function, whenever a store changes, doing whatever you need to do when that store changes.

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