Several large refactorings

* Refactor location of all data files to include a subdirectory
  for the mod version
* Update the BaseModel class to allow models to load themselves from
  any text file (whether JSON or CG)
* Refactor ModPack & ModVersion to move the laoding functionality in
  one into the other (and use the new stuff in BaseModel)
* Move the parser versions into a new subdirectory which allows for
  future additional parsers
This commit is contained in:
Andrew Miner
2015-01-16 20:26:54 -08:00
parent c14997ef90
commit 5f57622cf3
954 changed files with 284 additions and 1239 deletions
+49 -2
View File
@@ -5,22 +5,69 @@ Copyright (c) 2014-2015 by Redwood Labs
All rights reserved.
###
{Event} = require '../constants'
{ModelState} = require '../constants'
########################################################################################################################
module.exports = class BaseModel extends Backbone.Model
constructor: (attributes={}, options={})->
options.allowedSyncMethods = []
super attributes, options
makeGetter = (name)-> return -> @get name
makeSetter = (name)-> return (value)-> @set name, value
for name in _.keys attributes
for name, value of attributes
continue if name is 'id'
Object.defineProperty this, name, get:makeGetter(name), set:makeSetter(name)
@allowedSyncMethods = options.allowedSyncMethods
@state = ModelState.unloaded
@on 'request', => @state = ModelState.loading
@on 'sync', => @state = ModelState.loaded
@on 'error', => @state = ModelState.error
@loading = null
# Public Methods ###############################################################################
# Event Methods ################################################################################
onLoadSucceeded: (text, status, xhr)->
try
@set @parse text
@trigger Event.sync, this, text
catch e
logger.error "A parsing error occured: #{e.stack}"
@onLoadFailed e.message, 'parsing failed', xhr
onLoadFailed: (error, status, xhr)->
logger.error "#{@constructor.name} (#{@cid}) failed to load: status:#{status}, message:#{error}"
@trigger Event.error, this, error
# Backbone.Model Overrides #####################################################################
sync: -> # do nothing
fetch: ->
url = @url()
logger.info "#{@constructor.name} (#{@cid}) reading from url: #{url}"
@trigger Event.request, this
@loading = w.promise (resolve, reject)=>
$.ajax
url: url
dataType: 'text'
success: (text, status, xhr)=> resolve @onLoadSucceeded text, status, xhr
error: (xhr, status, error)=> reject @onLoadFailed error, status, xhr
return @loading
parse: (text)->
return JSON.parse text
sync: (method, model)->
throw new Error "#{@constructor.name} (#{@cid}) is not permitted to #{method}"
trigger: (name)->
logger.trace "#{@constructor.name}.#{@cid} triggered a \"#{name}\" event"