Improved @_fillUp(), added protection for endless loops.
This commit is contained in:
parent
aae37c735b
commit
ba44cb18b8
|
@ -525,19 +525,29 @@ class App.Model extends Spine.Model
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
||||||
@_fillUp: (data) ->
|
@_fillUp: (data, classNames = []) ->
|
||||||
|
|
||||||
# fill up via relations
|
# fill up via relations
|
||||||
if App[ @className ].configure_attributes
|
return data if !App[ @className ].configure_attributes
|
||||||
for attribute in App[ @className ].configure_attributes
|
for attribute in App[ @className ].configure_attributes
|
||||||
|
|
||||||
|
# lookup relations
|
||||||
if attribute.relation
|
if attribute.relation
|
||||||
|
|
||||||
|
# relations if if not calling object, to prevent loops
|
||||||
|
if !_.contains(classNames, @className)
|
||||||
|
|
||||||
|
# only if relation model exists
|
||||||
if App[ attribute.relation ]
|
if App[ attribute.relation ]
|
||||||
withoutId = attribute.name.substr( 0, attribute.name.length - 3 )
|
withoutId = attribute.name.substr( 0, attribute.name.length - 3 )
|
||||||
if attribute.name.substr( attribute.name.length - 3, attribute.name.length ) is '_id'
|
if attribute.name.substr( attribute.name.length - 3, attribute.name.length ) is '_id'
|
||||||
if data[attribute.name]
|
if data[attribute.name]
|
||||||
|
|
||||||
|
# only if relation record exists in collection
|
||||||
if App[ attribute.relation ].exists( data[attribute.name] )
|
if App[ attribute.relation ].exists( data[attribute.name] )
|
||||||
|
classNames.push @className
|
||||||
item = App[ attribute.relation ].find( data[attribute.name] )
|
item = App[ attribute.relation ].find( data[attribute.name] )
|
||||||
item = App[ attribute.relation ]._fillUp(item)
|
item = App[ attribute.relation ]._fillUp(item, classNames)
|
||||||
data[ withoutId ] = item
|
data[ withoutId ] = item
|
||||||
else
|
else
|
||||||
console.log("ERROR, cant find #{ attribute.name } App.#{ attribute.relation }.find(#{ data[attribute.name] }) for '#{ data.constructor.className }' #{ data.displayName() }")
|
console.log("ERROR, cant find #{ attribute.name } App.#{ attribute.relation }.find(#{ data[attribute.name] }) for '#{ data.constructor.className }' #{ data.displayName() }")
|
||||||
|
|
Loading…
Reference in a new issue