Skip to content

Instantly share code, notes, and snippets.

@fermion
Forked from gf3/model.js.coffee
Created October 24, 2012 19:10
Show Gist options
  • Select an option

  • Save fermion/3948185 to your computer and use it in GitHub Desktop.

Select an option

Save fermion/3948185 to your computer and use it in GitHub Desktop.

Revisions

  1. @gf3 gf3 revised this gist Oct 24, 2012. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions model.js.coffee
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    @instance_store = {}

    class App.Model extends Backbone.Model
    @@ -24,10 +23,6 @@ class App.Model extends Backbone.Model
    _getJsonRoot: ->
    @jsonRoot ||= @constructor.toString().match(/^function ([\w]+)/i)[1].toLowerCase()

    _parseDate: (str) ->
    return str if _.isDate str
    new Date Date.parse(str)

    #--------------------------------------------
    # INSTANCE STORE
    #--------------------------------------------
  2. @gf3 gf3 created this gist Oct 24, 2012.
    38 changes: 38 additions & 0 deletions model.js.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@

    @instance_store = {}

    class App.Model extends Backbone.Model

    cache_enabled: false
    cache_key: "id"

    constructor: (attr) ->
    @on "destroy", @_removeFromStore

    return super unless attr?.id
    id = attr.id
    klass = @_getJsonRoot()
    instance_store[klass] ||= {}

    return(
    if instance_store[klass][id]
    instance_store[klass][id]
    else
    super
    instance_store[klass][id] = this)

    _getJsonRoot: ->
    @jsonRoot ||= @constructor.toString().match(/^function ([\w]+)/i)[1].toLowerCase()

    _parseDate: (str) ->
    return str if _.isDate str
    new Date Date.parse(str)

    #--------------------------------------------
    # INSTANCE STORE
    #--------------------------------------------

    _removeFromStore: =>
    klass = @_getJsonRoot()
    delete instance_store[klass]?[@id]
    return