Skip to content

Instantly share code, notes, and snippets.

@hectorlorenzo
Last active July 17, 2016 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hectorlorenzo/c0f3cd282a7c35c59653e2ee7bee2b47 to your computer and use it in GitHub Desktop.
Save hectorlorenzo/c0f3cd282a7c35c59653e2ee7bee2b47 to your computer and use it in GitHub Desktop.
import ConstantsWrapper from './constantsWrapper'
export default {
items: [],
/**
* We define a set of namespaced constants to avoid collision of EventEmitter
* between modules
*
* @return {Object} Dictionary of namespaced actions
*/
constants () {
return ConstantsWrapper (this.namespace)
},
/**
* This is the first task Jeeves has to accomplish: based on the classnames
* passed from the module, we generate an array of Promises. Only when all
* the API calls have been resolvedm then we can notify the components that
* the application is ready, and start doing their stuff.
*
* @param {Array} classNames Array of strings containing the name of the
* different modules this Jeeves has to deal with.
* @return {Promise} Promise-like function containing the response from the
* API
*/
start (classNames) {
if (classNames === null || typeof classNames !== 'array') {
throw new Error('"classNames" should be declared as an array')
return false
}
// We map all the classNames into Promises from the API
let operations = classNames.map((cn) => {
return this.api[cn].get()
})
// When all operations are done
Promise.all(operations)
.then((response) => {
this.items = response.map((r) => {
return r.data.data
})
this.emitReady()
})
},
getItems (className) {
// 1. Call the API, who should return a Promise
// 2. Emit an update, which should be captured by components
// 3. Return the response as a Promise
if (!className) {
throw new Error('You should declare the className')
return false
}
return this.api[className].get()
.then((response) => {
this.items[className] = response.data.data
this.notify('update')
return response
})
},
updateItems (className, itemId, itemData) {
// 1. Check that everything is in place and passed correctly
// 2. Call the API to update the items
// 3. Update the items
// 3. Emit an update
// 4. Return the response as a Promise
if (!className) {
throw new Error('You should declare the className')
return false
}
if (!itemId) {
throw new Error('You should declare the item ID to update it')
return false
}
if (itemData === null || typeof itemData !== 'object') {
throw new Error('You should declare the item data to update it, and it' +
'and it should be an object')
return false
}
return this.api[className].update(itemId, itemData)
.then((response) => {
this.items[className] = response.data.data
this.notify('update')
return response
})
},
createItems (itemData) {
if (itemData === null || typeof itemData !== 'object') {
throw new Error('You should declare the item data to create it, and it' +
'and it should be an object')
return false
}
return this.api[className].create(itemData)
.then((response) => {
this.items[className] = response.data.data
this.notify('update')
return response
})
},
deleteItems (itemId) {
if (!itemId) {
throw new Error('You should declare the item ID to delete')
return false
}
return this.api[className].delete(itemId)
.then((response) => {
this.items[className] = response.data.data
this.notify('delete')
return response
})
},
notify (order) {
this.emit(this.constants[order])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment