Fix builds lists for Ember Data

This commit is contained in:
Piotr Sarnacki 2015-01-28 11:14:36 +01:00
parent ec24b21e81
commit ac25220ef2
5 changed files with 73 additions and 94 deletions

View File

@ -14,8 +14,15 @@ Travis.BuildsController = Em.ArrayController.extend
showMore: ->
id = @get('repo.id')
number = @get('lastObject.number')
@get('content').load Travis.Build.olderThanNumber(id, number, @get('tab'))
@get('content').load @olderThanNumber(id, number, @get('tab'))
displayShowMoreButton: (->
@get('tab') != 'branches' and parseInt(@get('lastObject.number')) > 1
).property('tab', 'lastObject.number')
olderThanNumber: (id, number, type) ->
options = { repository_id: id, after_number: number }
if type?
options.event_type = type.replace(/s$/, '') # poor man's singularize
@store.find('build', options)

View File

@ -1,10 +1,12 @@
colorForState = Travis.Helpers.colorForState
Controller = Em.ObjectController.extend(Travis.GithubUrlProperties,
needs: ['builds']
isPullRequestsListBinding: 'controllers.builds.isPullRequestsList'
buildBinding: 'content'
color: (->
Travis.Helpers.colorForState(@get('build.state'))
colorForState(@get('build.state'))
).property('build.state')
)

View File

@ -106,19 +106,3 @@ Travis.Build = Travis.Model.extend DurationCalculations,
if finishedAt = @get('finishedAt')
moment(finishedAt).format('lll')
).property('finishedAt')
@Travis.Build.reopenClass
byRepoId: (id, parameters) ->
@find($.extend(parameters || {}, repository_id: id))
branches: (options) ->
@find repository_id: options.repoId, branches: true
olderThanNumber: (id, build_number, type) ->
console.log type
# TODO fix this api and use some kind of pagination scheme
options = { repository_id: id, after_number: build_number }
if type?
options.event_type = type.replace(/s$/, '') # poor man's singularize
@find(options)

View File

@ -2,6 +2,7 @@ require 'travis/expandable_record_array'
require 'travis/model'
require 'helpers/helpers'
ExpandableRecordArray = Travis.ExpandableRecordArray
EnvVar = Travis.EnvVar
Build = Travis.Build
SshKey = Travis.SshKey
@ -64,50 +65,47 @@ Travis.Repo = Travis.Model.extend
array
).property()
allBuilds: (->
recordArray = Ember.RecordArray.create({ modelClass: Build, content: Ember.A([]) })
Build.registerRecordArray(recordArray)
recordArray
).property()
builds: (->
id = @get('id')
builds = Build.byRepoId id, event_type: 'push'
builds = @store.find('build', event_type: 'push', repository_id: id)
# TODO: move to controller
array = ExpandableRecordArray.create
type: Build
type: 'build'
content: Ember.A([])
array.load(builds)
id = @get('id')
array.observe(@get('allBuilds'), (build) -> build.get('isLoaded') && build.get('repo.id') == id && !build.get('isPullRequest') )
array.observe(@store.all('build'), (build) -> build.get('isLoaded') && build.get('repo.id') == id && !build.get('isPullRequest') )
array
).property()
pullRequests: (->
id = @get('id')
builds = Build.byRepoId id, event_type: 'pull_request'
builds = @store.find('build', event_type: 'pull_request', repository_id: id)
# TODO: move to controller
array = ExpandableRecordArray.create
type: Build
type: 'build'
content: Ember.A([])
array.load(builds)
id = @get('id')
array.observe(@get('allBuilds'), (build) -> build.get('isLoaded') && build.get('repo.id') == id && build.get('isPullRequest') )
array.observe(@store.all('build'), (build) -> build.get('isLoaded') && build.get('repo.id') == id && build.get('isPullRequest') )
array
).property()
branches: (->
Build.branches repoId: @get('id')
).property()
builds = @store.find 'build', repository_id: @get('id'), branches: true
events: (->
Event.byRepoId @get('id')
builds.then ->
builds.set 'isLoaded', true
builds
).property()
owner: (->

View File

@ -1,60 +1,48 @@
#Travis.ExpandableRecordArray = Ember.RecordArray.extend
# isLoaded: false
# isLoading: false
#
# promise: (->
# console.log 'promise'
# self = this
# new Ember.RSVP.Promise (resolve, reject) ->
# console.log 'inside promise'
# observer = ->
# console.log 'observer', self.get('isLoaded')
# if self.get('isLoaded')
# console.log 'resolve'
# resolve(self)
# self.removeObserver('isLoaded', observer)
# true
#
# unless observer()
# self.addObserver 'isLoaded', observer
# ).property()
#
# load: (array) ->
# @set 'isLoading', true
# self = this
#
# observer = ->
# if @get 'isLoaded'
# content = self.get 'content'
#
# array.removeObserver 'isLoaded', observer
# array.forEach (record) ->
# self.pushObject(record) unless self.contains(record)
#
# self.set 'isLoading', false
# self.set 'isLoaded', true
#
# array.addObserver 'isLoaded', observer
#
# observe: (collection, filterWith) ->
# @set 'filterWith', filterWith
# collection.addArrayObserver this,
# willChange: 'observedArrayWillChange'
# didChange: 'observedArraydidChange'
#
# observedArrayWillChange: (array, index, removedCount, addedCount) ->
# removedObjects = array.slice index, index + removedCount
# for object in removedObjects
# @removeObject(object)
#
# observedArraydidChange: (array, index, removedCount, addedCount) ->
# addedObjects = array.slice index, index + addedCount
# for object in addedObjects
# # TODO: I'm not sure why deleted objects get here, but I'll just filter them
# # for now
# if !object.get('isDeleted') && @get('filterWith').call(this, object)
# @pushObject(object) unless @contains(object)
#
# pushObject: (record) ->
# if content = @get('content')
# content.pushObject(record) unless content.contains(record)
Travis.ExpandableRecordArray = DS.RecordArray.extend
isLoaded: false
isLoading: false
promise: (->
self = this
new Ember.RSVP.Promise (resolve, reject) ->
observer = ->
if self.get('isLoaded')
resolve(self)
self.removeObserver('isLoaded', observer)
true
unless observer()
self.addObserver 'isLoaded', observer
).property()
load: (array) ->
@set 'isLoading', true
array.then =>
array.forEach (record) =>
@pushObject(record) unless @contains(record)
@set 'isLoading', false
@set 'isLoaded', true
observe: (collection, filterWith) ->
@set 'filterWith', filterWith
collection.addArrayObserver this,
willChange: 'observedArrayWillChange'
didChange: 'observedArraydidChange'
observedArrayWillChange: (array, index, removedCount, addedCount) ->
removedObjects = array.slice index, index + removedCount
for object in removedObjects
@removeObject(object)
observedArraydidChange: (array, index, removedCount, addedCount) ->
addedObjects = array.slice index, index + addedCount
for object in addedObjects
# TODO: I'm not sure why deleted objects get here, but I'll just filter them
# for now
if !object.get('isDeleted') && @get('filterWith').call(this, object)
@pushObject(object) unless @contains(object)
pushObject: (record) ->
if content = @get('content')
content.pushObject(record) unless content.contains(record)