Complete the RecipeBook controller

This commit is contained in:
Andrew Miner
2014-12-23 09:12:02 -08:00
parent 9fc0407fe4
commit b05fc4c5c9
19 changed files with 205 additions and 50 deletions
+38 -21
View File
@@ -5,9 +5,10 @@
# All rights reserved.
###
BaseModel = require './base_model'
{Event} = require '../constants'
RecipeBookParser = require './recipe_book_parser'
BaseModel = require './base_model'
{DefaultBookUrls} = require '../constants'
{Event} = require '../constants'
RecipeBookParser = require './recipe_book_parser'
########################################################################################################################
@@ -18,35 +19,51 @@ module.exports = class RecipeCatalog extends BaseModel
super attributes, options
@_parser = new RecipeBookParser
if @books.length is 0
@loadAllBooks DefaultBookUrls
# Public Methods ###############################################################################
loadBook: (url)->
@trigger Event.book.load.started, this, url
$.ajax
url: url
dataType: 'json'
success: (data, status, xhr)=> @onBookLoaded(data, status, xhr)
error: (xhr, status, error)=> @onBookLoadFailed(error, status, xhr)
w.promise (resolve, reject)=>
@trigger Event.load.started, this, url
$.ajax
url: url
dataType: 'json'
success: (data, status, xhr)=>
resolve @onBookLoaded(url, data, status, xhr)
error: (xhr, status, error)=>
reject @onBookLoadFailed(url, error, status, xhr)
loadAllBooks: (urlList)->
promises = (@loadBook(url) for url in urlList)
return w.settle promises
# Event Methods ################################################################################
onBookLoaded: (data, status, xhr)->
onBookLoaded: (url, data, status, xhr)->
try
@books.push @_parser.parse data
_(@books).sortBy 'name'
book = @_parser.parse data
@books.push book
@books.sort (a, b)->
return 0 if a.modName is b.modName
return if a.modName < b.modName then -1 else +1
logger.info "loaded recipe book: #{book}"
@trigger Event.book.load.succeeded, this, book
@trigger Event.book.load.finished, this
logger.info "loaded recipe book from #{url}: #{book}"
@trigger Event.load.succeeded, this, book
@trigger Event.load.finished, this
@trigger Event.change, this
return book
catch e
@onBookLoadFailed error, status, xhr
@onBookLoadFailed url, e, status, xhr
onBookLoadFailed: (error, status, xhr)->
logger.error "failed to load recipe book: #{error}"
@trigger Event.book.load.failed, this, error.message
@trigger Event.book.load.finished, this
onBookLoadFailed: (url, error, status, xhr)->
message = if error.stack? then error.stack else error
logger.error "failed to load recipe book from #{url}: #{message}"
@trigger Event.load.failed, this, error.message
@trigger Event.load.finished, this
return error
# Object Overrides #############################################################################