| 1 | 1 | var Promise = require('es6-promise').Promise |
| 2 | 1 | var Flux = require('../../flux') |
| 3 | 1 | var actionTypes = require('./action-types') |
| 4 | | |
| 5 | | /** |
| 6 | | * @param {Object} model |
| 7 | | * @param {String} model.entity |
| 8 | | * @param {Function} model.save |
| 9 | | * @param {Function} model.fetch |
| 10 | | * @param {Function} model.fetchAll |
| 11 | | * @param {Function} model.delete |
| 12 | | * @return {Object} |
| 13 | | */ |
| 14 | 1 | module.exports = function(model) { |
| 15 | 21 | var entity = model.entity; |
| 16 | 21 | var apiActions = {} |
| 17 | | |
| 18 | 21 | apiActions.fetch = function(params) { |
| 19 | 6 | Flux.dispatch(actionTypes.API_FETCH_START, { |
| 20 | | model: model, |
| 21 | | method: 'fetch', |
| 22 | | params: params, |
| 23 | | }) |
| 24 | 6 | return model.fetch(params).then( |
| 25 | | onFetchSuccess.bind(null, model, params), |
| 26 | | onFetchFail.bind(null, model, params) |
| 27 | | ) |
| 28 | | } |
| 29 | | |
| 30 | 21 | apiActions.fetchAll = function(params) { |
| 31 | 8 | Flux.dispatch(actionTypes.API_FETCH_START, { |
| 32 | | model: model, |
| 33 | | method: 'fetchAll', |
| 34 | | params: params, |
| 35 | | }) |
| 36 | 8 | return model.fetchAll(params).then( |
| 37 | | onFetchSuccess.bind(null, model, params), |
| 38 | | onFetchFail.bind(null, model, params) |
| 39 | | ) |
| 40 | | } |
| 41 | | |
| 42 | 21 | apiActions.save = function(params) { |
| 43 | 5 | Flux.dispatch(actionTypes.API_SAVE_START, { |
| 44 | | params: params, |
| 45 | | }) |
| 46 | 5 | return model.save(params).then( |
| 47 | | onSaveSuccess.bind(null, model, params), |
| 48 | | onSaveFail.bind(null, model, params) |
| 49 | | ) |
| 50 | | } |
| 51 | | |
| 52 | 21 | apiActions['delete'] = function(params) { |
| 53 | 4 | Flux.dispatch(actionTypes.API_DELETE_START, { |
| 54 | | params: params, |
| 55 | | }) |
| 56 | 4 | return model['delete'](params).then( |
| 57 | | onDeleteSuccess.bind(null, model, params), |
| 58 | | onDeleteFail.bind(null, model, params) |
| 59 | | ) |
| 60 | | } |
| 61 | | |
| 62 | 21 | return apiActions |
| 63 | | } |
| 64 | | |
| 65 | | /** |
| 66 | | * Handler for API fetch success, dispatches flux action to store the fetched |
| 67 | | * result in the api cache |
| 68 | | * @param {Model} model |
| 69 | | * @param {*} params used to call the `model.fetch(params)` |
| 70 | | * @param {Object} result |
| 71 | | * @return {Object} |
| 72 | | */ |
| 73 | 1 | function onFetchSuccess(model, params, result) { |
| 74 | 10 | Flux.dispatch(actionTypes.API_FETCH_SUCCESS, { |
| 75 | | model: model, |
| 76 | | params: params, |
| 77 | | result: result, |
| 78 | | }) |
| 79 | 10 | return result |
| 80 | | } |
| 81 | | |
| 82 | | /** |
| 83 | | * Handler for API fetch success, dispatches flux action to store the fetched |
| 84 | | * result in the api cache |
| 85 | | * @param {Model} model |
| 86 | | * @param {*} params used to call the `model.fetch(params)` |
| 87 | | * @param {*} reason |
| 88 | | * @return {Object} |
| 89 | | */ |
| 90 | 1 | function onFetchFail(model, params, reason) { |
| 91 | 4 | Flux.dispatch(actionTypes.API_FETCH_FAIL, { |
| 92 | | model: model, |
| 93 | | params: params, |
| 94 | | reason: reason, |
| 95 | | }) |
| 96 | 4 | return Promise.reject(reason) |
| 97 | | } |
| 98 | | |
| 99 | | /** |
| 100 | | * Handler for API save success, dispatches flux action to update the store with the |
| 101 | | * saved instance |
| 102 | | * @param {Model} model |
| 103 | | * @param {*} params used to call the `model.save(params)` |
| 104 | | * @param {Object} result |
| 105 | | * @return {Object} |
| 106 | | */ |
| 107 | 1 | function onSaveSuccess(model, params, result) { |
| 108 | 3 | Flux.dispatch(actionTypes.API_SAVE_SUCCESS, { |
| 109 | | model: model, |
| 110 | | params: params, |
| 111 | | result: result, |
| 112 | | }) |
| 113 | 3 | return result |
| 114 | | } |
| 115 | | |
| 116 | | /** |
| 117 | | * Handler for API save success, dispatches flux action to update the store with the |
| 118 | | * saved instance |
| 119 | | * @param {Model} model |
| 120 | | * @param {*} params used to call the `model.save(params)` |
| 121 | | * @param {*} reason |
| 122 | | * @return {Object} |
| 123 | | */ |
| 124 | 1 | function onSaveFail(model, params, reason) { |
| 125 | 2 | Flux.dispatch(actionTypes.API_SAVE_FAIL, { |
| 126 | | model: model, |
| 127 | | params: params, |
| 128 | | reason: reason, |
| 129 | | }) |
| 130 | 2 | return Promise.reject(reason) |
| 131 | | } |
| 132 | | |
| 133 | | /** |
| 134 | | * Handler for API delete success, dispatches flux action to remove the instance from the stores |
| 135 | | * @param {Model} model |
| 136 | | * @param {*} params used to call the `model.delete(params)` |
| 137 | | * @param {Object} result |
| 138 | | * @return {Object} |
| 139 | | */ |
| 140 | 1 | function onDeleteSuccess(model, params, result) { |
| 141 | 2 | Flux.dispatch(actionTypes.API_DELETE_SUCCESS, { |
| 142 | | model: model, |
| 143 | | params: params, |
| 144 | | result: result, |
| 145 | | }) |
| 146 | 2 | return result |
| 147 | | } |
| 148 | | |
| 149 | | /** |
| 150 | | * Handler for API delete fail |
| 151 | | * @param {Model} model |
| 152 | | * @param {*} params used to call the `model.delete(params)` |
| 153 | | * @param {Object} result |
| 154 | | * @return {Object} |
| 155 | | */ |
| 156 | 1 | function onDeleteFail(model, params, reason) { |
| 157 | 2 | Flux.dispatch(actionTypes.API_DELETE_FAIL, { |
| 158 | | model: model, |
| 159 | | params: params, |
| 160 | | reason: reason, |
| 161 | | }) |
| 162 | 2 | return Promise.reject(reason) |
| 163 | | } |
| 164 | | |
| 165 | | |