Sha256: c448c036cffe7ab625fdfd0876933c9077470fb8cc165882336b41bd86c9e1ac

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

#  * Require ./../underscore/underscore.string
#  * Require ./../underscore/underscore.inflection
#= require ./base

class Ultimate.Backbone.Model extends Backbone.Model

  readyDeferred: null
  loaded: false
  loadedTimeStamp: null
  expireTime: Infinity

  constructor: (attributes, options = {}) ->
    @expireTime = options.expireTime  if options.expireTime?
    @on 'sync', =>
      @loadedTimeStamp = new Date()
      @loaded = true
    super

  ready: (callback, fetchOptions) ->
    lifeTime = if @loadedTimeStamp then (new Date() - @loadedTimeStamp) else 0
    if expired = lifeTime > @expireTime
      @readyDeferred = null
    if @id and (not @loaded or expired)
      @readyDeferred ||= @fetch(fetchOptions)
      @readyDeferred.done =>
        callback.apply @
    else
      callback.apply @

  abort: ->
    if @readyDeferred?
      if @readyDeferred.state() is 'pending'
        @readyDeferred.abort()
        @readyDeferred = null

  singular: ->
    modelName = @constructor.modelName or @modelName or @className or @constructor.name or 'Model'
    _.singularize(_.string.underscored(modelName))

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ultimate-base-0.7.2 app/assets/javascripts/ultimate/backbone/model.js.coffee
ultimate-base-0.6.2 app/assets/javascripts/ultimate/backbone/model.js.coffee
ultimate-base-0.6.0 app/assets/javascripts/ultimate/backbone/model.js.coffee