Update Travis.Model to work with Ember Model
This commit is contained in:
parent
ccfd116f08
commit
c0cb223aeb
|
@ -71,7 +71,7 @@ require 'travis/model'
|
||||||
requeue: ->
|
requeue: ->
|
||||||
Travis.ajax.post '/requests', build_id: @get('id')
|
Travis.ajax.post '/requests', build_id: @get('id')
|
||||||
|
|
||||||
isAttributeLoaded: (key) ->
|
isPropertyLoaded: (key) ->
|
||||||
if ['_duration', 'finishedAt'].contains(key) && !@get('isFinished')
|
if ['_duration', 'finishedAt'].contains(key) && !@get('isFinished')
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
|
|
|
@ -22,10 +22,10 @@ require 'travis/model'
|
||||||
|
|
||||||
repoSlugDidChange: (->
|
repoSlugDidChange: (->
|
||||||
if slug = @get('repoSlug')
|
if slug = @get('repoSlug')
|
||||||
@get('store').loadIncomplete(Travis.Repo, {
|
Travis.Repo.load([{
|
||||||
id: @get('repoId'),
|
id: @get('repoId'),
|
||||||
slug: slug
|
slug: slug
|
||||||
}, { skipIfExists: true })
|
}])
|
||||||
).observes('repoSlug')
|
).observes('repoSlug')
|
||||||
|
|
||||||
log: ( ->
|
log: ( ->
|
||||||
|
@ -98,7 +98,7 @@ require 'travis/model'
|
||||||
Travis.pusher.unsubscribe "job-#{@get('id')}"
|
Travis.pusher.unsubscribe "job-#{@get('id')}"
|
||||||
).observes('state')
|
).observes('state')
|
||||||
|
|
||||||
isAttributeLoaded: (key) ->
|
isPropertyLoaded: (key) ->
|
||||||
if ['finishedAt'].contains(key) && !@get('isFinished')
|
if ['finishedAt'].contains(key) && !@get('isFinished')
|
||||||
return true
|
return true
|
||||||
else if key == 'startedAt' && @get('state') == 'created'
|
else if key == 'startedAt' && @get('state') == 'created'
|
||||||
|
|
55
assets/scripts/lib/travis/adapter.coffee
Normal file
55
assets/scripts/lib/travis/adapter.coffee
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
Travis.Adapter = Ember.RESTAdapter.extend
|
||||||
|
ajax: (url, params, method) ->
|
||||||
|
Travis.ajax.ajax(url, method || 'get', data: params)
|
||||||
|
|
||||||
|
findMany: (klass, records, ids) ->
|
||||||
|
url = @buildURL(klass) + '?' + ids.map( (id) -> "ids[]=#{id}" ).join('&')
|
||||||
|
|
||||||
|
self = this
|
||||||
|
@ajax(url).then (data) ->
|
||||||
|
self.didFindMany(klass, records, data)
|
||||||
|
|
||||||
|
didFindMany: (klass, records, data) ->
|
||||||
|
collectionKey = Ember.get(klass, 'collectionKey')
|
||||||
|
dataToLoad = if collectionKey then data[collectionKey] else data
|
||||||
|
|
||||||
|
@sideload(klass, data)
|
||||||
|
records.load(klass, dataToLoad)
|
||||||
|
|
||||||
|
buildURL: ->
|
||||||
|
@_super.apply(this, arguments).replace(/\.json$/, '')
|
||||||
|
|
||||||
|
didFind: (record, id, data) ->
|
||||||
|
@sideload(record.constructor, data)
|
||||||
|
@_super(record, id, data)
|
||||||
|
|
||||||
|
didFindAll: (klass, records, data) ->
|
||||||
|
@sideload(klass, data)
|
||||||
|
@_super(klass, records, data)
|
||||||
|
|
||||||
|
didFindQuery: (klass, records, params, data) ->
|
||||||
|
@sideload(klass, data)
|
||||||
|
@_super(klass, records, params, data)
|
||||||
|
|
||||||
|
didCreateRecord: (record, data) ->
|
||||||
|
@sideload(record.constructor, data)
|
||||||
|
@_super(record, data)
|
||||||
|
|
||||||
|
didSaveRecord: (record, data) ->
|
||||||
|
@sideload(record.constructor, data)
|
||||||
|
@_super(record, data)
|
||||||
|
|
||||||
|
didDeleteRecord: (record, data) ->
|
||||||
|
@sideload(record.constructor, data)
|
||||||
|
@_super(record, data)
|
||||||
|
|
||||||
|
sideload: (klass, data) ->
|
||||||
|
for name, records of data
|
||||||
|
records = [records] unless Ember.isArray(records)
|
||||||
|
|
||||||
|
# we need to skip records of type, which is loaded by adapter already
|
||||||
|
if (type = Ember.get(Travis, 'mappings')[name]) && type != klass
|
||||||
|
for record in records
|
||||||
|
type.findFromCacheOrLoad(record)
|
||||||
|
|
||||||
|
|
|
@ -1,41 +1,92 @@
|
||||||
@Travis.Model = DS.Model.extend
|
get = Ember.get
|
||||||
|
set = Ember.set
|
||||||
|
|
||||||
|
Array.prototype.diff = (a) ->
|
||||||
|
this.filter (i) -> !(a.indexOf(i) > -1)
|
||||||
|
|
||||||
|
|
||||||
|
@Travis.Model = Ember.Model.extend
|
||||||
|
id: Ember.attr('number')
|
||||||
|
|
||||||
init: ->
|
init: ->
|
||||||
@loadedAttributes = []
|
|
||||||
@_super.apply this, arguments
|
@_super.apply this, arguments
|
||||||
|
this
|
||||||
|
|
||||||
|
merge: (hash) ->
|
||||||
|
data = @get('_data')
|
||||||
|
Ember.merge(data, hash)
|
||||||
|
@notifyPropertyChange('_data')
|
||||||
|
|
||||||
|
dataKey: (key) ->
|
||||||
|
meta = @constructor.metaForProperty(key)
|
||||||
|
if meta.isRelationship && !meta.options?.key?
|
||||||
|
type = meta.type
|
||||||
|
if typeof type == "string"
|
||||||
|
type = Ember.get(Ember.lookup, type)
|
||||||
|
|
||||||
|
if meta.kind == 'belongsTo'
|
||||||
|
return type.singularName() + '_id'
|
||||||
|
else
|
||||||
|
return type.singularName() + '_ids'
|
||||||
|
|
||||||
|
@_super(key)
|
||||||
|
|
||||||
|
load: (id, hash) ->
|
||||||
|
@loadedAttributes = []
|
||||||
|
@loadedRelationships = []
|
||||||
|
|
||||||
|
attributes = this.attributes || []
|
||||||
|
relationships = this.relationships || []
|
||||||
|
|
||||||
|
for key in attributes
|
||||||
|
dataKey = @dataKey(key)
|
||||||
|
if hash.hasOwnProperty(dataKey)
|
||||||
|
@loadedAttributes.pushObject(key)
|
||||||
|
|
||||||
|
for key in relationships
|
||||||
|
dataKey = @dataKey(key)
|
||||||
|
if hash.hasOwnProperty(dataKey)
|
||||||
|
@loadedRelationships.pushObject(key)
|
||||||
|
|
||||||
|
incomplete = Ember.EnumerableUtils.intersection(@loadedAttributes, attributes).length != attributes.length ||
|
||||||
|
Ember.EnumerableUtils.intersection(@loadedRelationships, relationships).length != relationships.length
|
||||||
|
|
||||||
|
#if incomplete
|
||||||
|
# properties = attributes.concat(relationships)
|
||||||
|
# loadedProperties = @loadedAttributes.concat(@loadedRelationships)
|
||||||
|
# diff = properties.diff(loadedProperties)
|
||||||
|
# #console.log(@constructor, 'with id', id, 'loaded as incomplete, info:', { diff: diff, attributes: loadedProperties, data: hash})
|
||||||
|
|
||||||
|
@set('incomplete', incomplete)
|
||||||
|
|
||||||
|
@_super(id, hash)
|
||||||
|
|
||||||
getAttr: (key, options) ->
|
getAttr: (key, options) ->
|
||||||
@needsCompletionCheck(key)
|
@needsCompletionCheck(key)
|
||||||
@_super.apply this, arguments
|
@_super.apply this, arguments
|
||||||
|
|
||||||
getBelongsTo: (key, type, meta) ->
|
getBelongsTo: (key, type, meta) ->
|
||||||
|
unless key
|
||||||
|
key = type.singularName() + '_id'
|
||||||
@needsCompletionCheck(key)
|
@needsCompletionCheck(key)
|
||||||
@_super.apply this, arguments
|
@_super(key, type, meta)
|
||||||
|
|
||||||
getHasMany: (key, type, meta) ->
|
getHasMany: (key, type, meta) ->
|
||||||
|
unless key
|
||||||
|
key = type.singularName() + '_ids'
|
||||||
@needsCompletionCheck(key)
|
@needsCompletionCheck(key)
|
||||||
@_super.apply this, arguments
|
@_super(key, type, meta)
|
||||||
|
|
||||||
needsCompletionCheck: (key) ->
|
needsCompletionCheck: (key) ->
|
||||||
if key && (@constructor.isAttribute(key) || @constructor.isRelationship(key)) &&
|
if key && (@isAttribute(key) || @isRelationship(key)) &&
|
||||||
@get('incomplete') && !@isAttributeLoaded(key)
|
@get('incomplete') && !@isPropertyLoaded(key)
|
||||||
@loadTheRest(key)
|
@loadTheRest(key)
|
||||||
|
|
||||||
update: (attrs) ->
|
isAttribute: (name) ->
|
||||||
$.each attrs, (key, value) =>
|
this.attributes.contains(name)
|
||||||
@set(key, value) unless key is 'id'
|
|
||||||
this
|
|
||||||
|
|
||||||
isAttributeLoaded: (name) ->
|
isRelationship: (name) ->
|
||||||
@get('store').isDataLoadedFor(this.constructor, @get('clientId'), name)
|
this.relationships.contains(name)
|
||||||
|
|
||||||
isComplete: (->
|
|
||||||
if @get 'incomplete'
|
|
||||||
@loadTheRest()
|
|
||||||
false
|
|
||||||
else
|
|
||||||
@set 'isCompleting', false
|
|
||||||
@get 'isLoaded'
|
|
||||||
).property('incomplete', 'isLoaded')
|
|
||||||
|
|
||||||
loadTheRest: (key) ->
|
loadTheRest: (key) ->
|
||||||
# for some weird reason key comes changed to a string and for some weird reason it even is called with
|
# for some weird reason key comes changed to a string and for some weird reason it even is called with
|
||||||
|
@ -43,32 +94,21 @@
|
||||||
return if !key || key == 'undefined'
|
return if !key || key == 'undefined'
|
||||||
|
|
||||||
message = "Load missing fields for #{@constructor.toString()} because of missing key '#{key}', cid: #{@get('clientId')}, id: #{@get('id')}"
|
message = "Load missing fields for #{@constructor.toString()} because of missing key '#{key}', cid: #{@get('clientId')}, id: #{@get('id')}"
|
||||||
if @constructor.isAttribute('state') && key != 'state'
|
if @isAttribute('state') && key != 'state'
|
||||||
message += ", in state: #{@get('state')}"
|
message += ", in state: #{@get('state')}"
|
||||||
console.log message
|
console.log message
|
||||||
return if @get('isCompleting')
|
return if @get('isCompleting')
|
||||||
@set 'isCompleting', true
|
@set 'isCompleting', true
|
||||||
|
|
||||||
unless @get('stateManager.currentState.path').match /^rootState.loaded.materializing/
|
@reload()
|
||||||
@reload()
|
|
||||||
@set 'incomplete', false
|
|
||||||
|
|
||||||
select: ->
|
select: ->
|
||||||
@constructor.select(@get('id'))
|
@constructor.select(@get('id'))
|
||||||
|
|
||||||
|
isPropertyLoaded: (name) ->
|
||||||
|
@loadedAttributes.contains(name) || @loadedRelationships.contains(name)
|
||||||
|
|
||||||
@Travis.Model.reopenClass
|
@Travis.Model.reopenClass
|
||||||
find: ->
|
|
||||||
if arguments.length == 0
|
|
||||||
Travis.store.findAll(this)
|
|
||||||
else
|
|
||||||
@_super.apply(this, arguments)
|
|
||||||
|
|
||||||
filter: (callback) ->
|
|
||||||
Travis.store.filter(this, callback)
|
|
||||||
|
|
||||||
load: (attrs) ->
|
|
||||||
Travis.store.load(this, attrs)
|
|
||||||
|
|
||||||
select: (id) ->
|
select: (id) ->
|
||||||
@find().forEach (record) ->
|
@find().forEach (record) ->
|
||||||
record.set('selected', record.get('id') == id)
|
record.set('selected', record.get('id') == id)
|
||||||
|
@ -87,18 +127,21 @@
|
||||||
name.replace(/([A-Z])/g, '_$1').toLowerCase().slice(1)
|
name.replace(/([A-Z])/g, '_$1').toLowerCase().slice(1)
|
||||||
|
|
||||||
pluralName: ->
|
pluralName: ->
|
||||||
Travis.store.adapter.pluralize(@singularName())
|
@singularName() + 's'
|
||||||
|
|
||||||
isAttribute: (name) ->
|
collectionKey: (->
|
||||||
Ember.get(this, 'attributes').has(name)
|
@pluralName()
|
||||||
|
).property()
|
||||||
|
|
||||||
isRelationship: (name) ->
|
rootKey: (->
|
||||||
Ember.get(this, 'relationshipsByName').has(name)
|
@singularName()
|
||||||
|
).property()
|
||||||
|
|
||||||
isHasManyRelationship: (name) ->
|
isModel: (->
|
||||||
if relationship = Ember.get(this, 'relationshipsByName').get(name)
|
true
|
||||||
relationship.kind == 'hasMany'
|
).property()
|
||||||
|
|
||||||
isBelongsToRelationship: (name) ->
|
isRecordLoaded: (id) ->
|
||||||
if relationship = Ember.get(this, 'relationshipsByName').get(name)
|
!!@_referenceForId(id).record
|
||||||
relationship.kind == 'belongsTo'
|
|
||||||
|
camelizeKeys: true
|
||||||
|
|
Loading…
Reference in New Issue
Block a user