From ba44cb18b86553155a7c0de09e165c07179fed10 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 27 Aug 2015 21:12:50 +0200 Subject: [PATCH] Improved @_fillUp(), added protection for endless loops. --- .../app/models/_application_model.js.coffee | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/app/models/_application_model.js.coffee b/app/assets/javascripts/app/models/_application_model.js.coffee index 53a9949e3..f1f06f60d 100644 --- a/app/assets/javascripts/app/models/_application_model.js.coffee +++ b/app/assets/javascripts/app/models/_application_model.js.coffee @@ -525,19 +525,29 @@ class App.Model extends Spine.Model return true - @_fillUp: (data) -> + @_fillUp: (data, classNames = []) -> # fill up via relations - if App[ @className ].configure_attributes - for attribute in App[ @className ].configure_attributes - if attribute.relation + return data if !App[ @className ].configure_attributes + for attribute in App[ @className ].configure_attributes + + # lookup relations + 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 ] withoutId = attribute.name.substr( 0, attribute.name.length - 3 ) if attribute.name.substr( attribute.name.length - 3, attribute.name.length ) is '_id' if data[attribute.name] + + # only if relation record exists in collection if App[ attribute.relation ].exists( data[attribute.name] ) + classNames.push @className item = App[ attribute.relation ].find( data[attribute.name] ) - item = App[ attribute.relation ]._fillUp(item) + item = App[ attribute.relation ]._fillUp(item, classNames) data[ withoutId ] = item else console.log("ERROR, cant find #{ attribute.name } App.#{ attribute.relation }.find(#{ data[attribute.name] }) for '#{ data.constructor.className }' #{ data.displayName() }")