Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Last active May 9, 2020 16:25
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simenbrekken/de69d3ce27ea5934c8b2 to your computer and use it in GitHub Desktop.
Save simenbrekken/de69d3ce27ea5934c8b2 to your computer and use it in GitHub Desktop.
Category Actions
'use strict';
var createAsyncActions = require('./createAsyncActions')
var CategoryAPI = require('../api/CategoryAPI')
var CategoryActions = createAsyncActions({
create: function(category) {
CategoryAPI
.create(category)
.then(CategoryActions.createCompleted)
.catch(function(error) {
CategoryActions.createFailed(error, category)
})
},
update: function(category) {
CategoryAPI
.update(category)
.then(CategoryActions.updateCompleted)
.catch(function(error) {
CategoryActions.updateFailed(error, category)
})
},
delete: function(category) {
CategoryAPI
.delete(category)
.then(function() {
CategoryActions.deleteCompleted(category)
})
.catch(function(error) {
CategoryActions.deleteFailed(error, category)
})
}
})
module.exports = CategoryActions
'use strict';
var createAction = require('reflux').createAction
var lodash = require('lodash')
function createAsyncActions(definitions) {
return lodash.reduce(definitions, function(actions, callback, name) {
actions[name] = createAction()
actions[name].listen(callback)
actions[name + 'Completed'] = createAction()
actions[name + 'Failed'] = createAction()
return actions
}, {})
}
module.exports = createAsyncActions
@simenbrekken
Copy link
Author

The CategoryAPI basically produces HTTP REST Promises that reject on status >= 300.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment