Created
September 13, 2016 14:58
-
-
Save sminutoli/0450e741dbb66f82bcf40e4635eeac8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('./API'); | |
require('./Item'); | |
require('./Category'); | |
const App = { | |
selector: '#app', | |
fetchAPI: API, | |
run(){ | |
this.fetchAPI | |
.categories() | |
.then(this.recieveCategories.bind(this)) // cómo se puede preservar el this dentro de las promises? bluebird soporta un bind() pero parece que ES2015 no :( | |
.then(this.addItemsToCategories.bind(this)) | |
.then(this.addCategoriesToList.bind(this)) | |
.then(this.listToHTML.bind(this)) | |
.then(this.renderHTML.bind(this)); | |
}, | |
recieveCategories(aJSON){ | |
return aJSON.map(Category.from, Category); | |
}, | |
addItemsToCategories(categories){ | |
return categories.map(this.addItemToCategory, this); // acá no se bien cómo hacer esto, quizá en dos pasos | |
}, | |
addItemToCategory(category){ | |
return category.addItem(Item.from(category.item)); | |
}, | |
addCategoriesToList(categories){ | |
return categories.reduce( (prev, act) => prev.addItem(act), CategoryList.from({}) ) | |
}, | |
listToHTML(categoryList){ | |
return categoryList.toHTML(); | |
}, | |
renderHTML(html){ | |
$(this.selector).html(html); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment