
In order to minimize ajax requests, I implemented isComplete property, which can be used to check if record is fetched from the API or if it was just partially loaded (for example by pusher event). This is nice in terms of requests reduction, but caries risk of showing incomplete data. This commit fixes this situation by saving which attributes were provided on "incomplete" load and triggering refresh when any unknown attribute is tried to be fetched. The implementation is really simple and will probably need refactoring, but I would like to test it in the wild before putting much more time into it.
88 lines
2.1 KiB
CoffeeScript
88 lines
2.1 KiB
CoffeeScript
@Travis.Model = DS.Model.extend
|
|
primaryKey: 'id'
|
|
id: DS.attr('number')
|
|
|
|
get: (name) ->
|
|
if @constructor.isAttribute(name) && @get('incomplete') && !@isAttributeLoaded(name)
|
|
@loadTheRest()
|
|
|
|
@_super.apply this, arguments
|
|
|
|
refresh: ->
|
|
if id = @get('id')
|
|
store = @get('store')
|
|
store.adapter.find store, @constructor, id
|
|
|
|
update: (attrs) ->
|
|
$.each attrs, (key, value) =>
|
|
@set(key, value) unless key is 'id'
|
|
this
|
|
|
|
isAttributeLoaded: (name) ->
|
|
@loadedAttributes.contains(name)
|
|
|
|
isComplete: (->
|
|
if @get 'incomplete'
|
|
@loadTheRest()
|
|
false
|
|
else
|
|
@set 'isCompleting', false
|
|
@get 'isLoaded'
|
|
).property('incomplete', 'isLoaded')
|
|
|
|
loadTheRest: ->
|
|
return if @get('isCompleting')
|
|
@set 'isCompleting', true
|
|
|
|
@refresh()
|
|
|
|
select: ->
|
|
@constructor.select(@get('id'))
|
|
|
|
@Travis.Model.reopenClass
|
|
find: ->
|
|
if arguments.length == 0
|
|
Travis.app.store.findAll(this)
|
|
else
|
|
@_super.apply(this, arguments)
|
|
|
|
filter: (callback) ->
|
|
Travis.app.store.filter(this, callback)
|
|
|
|
load: (attrs) ->
|
|
Travis.app.store.load(this, attrs)
|
|
|
|
select: (id) ->
|
|
@find().forEach (record) ->
|
|
record.set('selected', record.get('id') == id)
|
|
|
|
buildURL: (suffix) ->
|
|
base = @url || @pluralName()
|
|
Ember.assert('Base URL (' + base + ') must not start with slash', !base || base.toString().charAt(0) != '/')
|
|
Ember.assert('URL suffix (' + suffix + ') must not start with slash', !suffix || suffix.toString().charAt(0) != '/')
|
|
url = [base]
|
|
url.push(suffix) if (suffix != undefined)
|
|
url.join('/')
|
|
|
|
singularName: ->
|
|
parts = @toString().split('.')
|
|
name = parts[parts.length - 1]
|
|
name.replace(/([A-Z])/g, '_$1').toLowerCase().slice(1)
|
|
|
|
pluralName: ->
|
|
Travis.app.store.adapter.pluralize(@singularName())
|
|
|
|
isAttribute: (name) ->
|
|
unless @attributesSaved
|
|
@_saveAttributes()
|
|
@cachedAttributes.contains(name)
|
|
|
|
_saveAttributes: ->
|
|
@attributesSaved = true
|
|
|
|
cachedAttributes = []
|
|
@eachComputedProperty (name, meta) ->
|
|
cachedAttributes.pushObject name if meta.isAttribute
|
|
|
|
@cachedAttributes = cachedAttributes
|