Skip to content

Instantly share code, notes, and snippets.

@bebraw

bebraw/actions.js

Last active Oct 28, 2015
Embed
What would you like to do?
Reflux + Axios.
'use strict';
var Reflux = require('reflux');
var axios = require('axios');
var Actions = Reflux.createActions({
load: {
children: ['completed', 'failed']
}
});
Actions.load.listen(function() {
var that = this;
axios.get('/whatever').then(function(res) {
that.completed(res.data);
}).catch(this.failed);
});
module.exports = Actions;
'use strict';
var Reflux = require('reflux');
var actions = require('./actions');
module.exports = Reflux.createStore({
init: function() {
this.data = [];
this.listenTo(actions.load.completed, this.loadCompleted);
this.listenTo(actions.load.failed, this.loadFailed);
},
loadCompleted: function(data) {
this.data = data;
this.trigger(data);
},
loadFailed: function(err) {
console.error(err);
},
});
@dinodsaurus

This comment has been minimized.

Copy link

@dinodsaurus dinodsaurus commented Aug 20, 2015

Thanks for this example, one of the better ones I found online!

@mauriciosoares

This comment has been minimized.

Copy link

@mauriciosoares mauriciosoares commented Sep 4, 2015

👍

@mauriciosoares

This comment has been minimized.

Copy link

@mauriciosoares mauriciosoares commented Sep 4, 2015

Hey brebaw... for some reason when I tried to use this example, the initial "load" is not being triggered...

Do I have to manually trigger it in somewhere? Or Am I probably doing something wrong?

@mauriciosoares

This comment has been minimized.

Copy link

@mauriciosoares mauriciosoares commented Sep 5, 2015

God dammit, I got it now...

I have to trigger actions.load() to actually trigger the ajax call... I thought the first one was automatically triggered... my fault...

Thanks for the example :)

@freewayz

This comment has been minimized.

Copy link

@freewayz freewayz commented Sep 21, 2015

Wow this helped, but please how do you use this inside the react component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.