First spike of 'isComplete' functionality

When we get payload from pusher, we usually don't send the entire
record. Initially such records where fetched from server right away to
get missing data. This was done becuase Ember can't tell if given data
is complete or not and just assumes that the record is loaded.

To not fire unneeded request, this code sets incomplete flag on records
loaded from pusher and loads the rest of the data only if needed.
This commit is contained in:
Piotr Sarnacki 2012-10-13 20:47:21 +02:00
parent 7ccbba2959
commit c707135ccd
9 changed files with 61 additions and 19 deletions

View File

@ -8,6 +8,22 @@ Travis.Store = DS.Store.extend
revision: 4
adapter: Travis.RestAdapter.create()
load: (type, id, hash) ->
result = @_super.apply this, arguments
if result && result.clientId
# I assume that everything that goes through load is complete record
# representation, incomplete hashes from pusher go through merge()
record = @findByClientId type, result.clientId
record.set 'incomplete', false
record.set 'complete', true
# setting both incomplete and complete may be weird, but it's easier to
# work with both values. I need to check if record has already been completed
# and in order to do that, without having 'complete', I would need to check
# for incomplete == false, which looks worse
result
merge: (type, id, hash) ->
if hash == undefined
hash = id
@ -29,11 +45,7 @@ Travis.Store = DS.Store.extend
if record = recordCache[clientId]
record.send('didChangeData')
else
# that's nasty, but will do for now
# if event is triggered for a record
# that's not yet available, just use find
# to make a request to fetch it
clientId = @find(type, id).get('clientId')
clientId = @pushHash(hash, id, type)
if clientId
DATA_PROXY.savedData = hash
@ -59,9 +71,23 @@ Travis.Store = DS.Store.extend
_loadOne: (store, type, json) ->
root = type.singularName()
@adapter.sideload(store, type, json, root)
@merge(type, json[root])
@_updateAssociations(type, root, json[root])
# we get other types of records only on build, it comes with repository
# attached. I don't want to use store.sideload here as it will not use merge,
# if we need sideload becasue we have side records with other events it needs to
# be revised
if type == Travis.Build && json.repository
result = @_loadIncomplete(Travis.Repo, 'repository', json.repository)
@_loadIncomplete(type, root, json[root])
_loadIncomplete: (type, root, hash) ->
result = @merge(type, hash)
if result && result.clientId
record = @findByClientId(type, result.clientId)
unless record.get('complete')
record.set 'incomplete', true
@_updateAssociations(type, root, hash)
_loadMany: (store, type, json) ->
root = type.pluralName()

View File

@ -1,5 +1,5 @@
{{#with view}}
{{#if job.isLoaded}}
{{#if job.isComplete}}
<div {{bindAttr class="view.color"}}>
<dl id="summary">
<div class="left">

View File

@ -2,7 +2,7 @@
{{#if view.isEmpty}}
{{view Travis.ReposEmptyView}}
{{else}}
{{#if view.repo.isLoaded}}
{{#if view.repo.isComplete}}
{{#with view.repo}}
<h3>
<a {{bindAttr href="view.urlGithub"}}>{{slug}}</a>

View File

@ -55,8 +55,8 @@
currentItemBinding: 'build'
loading: (->
!@get('build.isLoaded')
).property('build.isLoaded')
!@get('build.isComplete')
).property('build.isComplete')
color: (->
Travis.Helpers.colorForResult(@get('build.result'))

View File

@ -6,8 +6,8 @@
repoBinding: 'controller.repo'
class: (->
'loading' if !@get('repo.isLoaded') && !@get('isEmpty')
).property('repo.isLoaded')
'loading' if !@get('repo.isComplete') && !@get('isEmpty')
).property('repo.isComplete')
isEmpty: (->
@get('repos.length') == 0

View File

@ -3,14 +3,30 @@
id: DS.attr('number')
refresh: ->
id = @get('id')
Travis.app.store.adapter.find(Travis.app.store, @constructor, id) if id
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
isComplete: (->
if @get 'incomplete'
@loadTheRest()
false
else
@set 'isCompleting', false
@get 'isLoaded'
).property('incomplete', 'isLoaded')
loadTheRest: ->
return if @get('isCompleting')
@set 'isCompleting', true
@refresh()
@Travis.Model.reopenClass
find: ->
if arguments.length == 0

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
f4d864ad
e60df424