From 1e311034a0934b63fb80e63190a1a933a8ee9d92 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Mon, 26 Feb 2018 11:40:57 +0100 Subject: [PATCH] Fixed issue #949 - Sometimes pending time is show as first ticket attribute. --- .../app/models/_application_model.coffee | 21 +++++++++++-- public/assets/tests/model.js | 31 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/app/models/_application_model.coffee b/app/assets/javascripts/app/models/_application_model.coffee index 660af96df..ba5b442d3 100644 --- a/app/assets/javascripts/app/models/_application_model.coffee +++ b/app/assets/javascripts/app/models/_application_model.coffee @@ -828,14 +828,29 @@ set new attributes of model (remove already available attributes) @updateAttributes: (attributes) -> return if !@className if _.isEmpty(@org_configure_attributes) - @org_configure_attributes = clone(@configure_attributes) + + # use jquery instead of ._clone() because we need a deep copy of the obj + @org_configure_attributes = $.extend(true, [], @configure_attributes) for attribute in attributes @attributes.push attribute.name - @configure_attributes.push attribute + + found = false + for attribute_model, index in @configure_attributes + continue if attribute_model.name != attribute.name + + @configure_attributes[index] = _.extend(attribute_model, attribute) + + found = true + break + + if !found + @configure_attributes.push attribute @resetAttributes: -> return if _.isEmpty(@org_configure_attributes) - @configure_attributes = @org_configure_attributes + + # use jquery instead of ._clone() because we need a deep copy of the obj + @configure_attributes = $.extend(true, [], @org_configure_attributes) @resetCallbacks: -> @SUBSCRIPTION_ITEM = {} diff --git a/public/assets/tests/model.js b/public/assets/tests/model.js index 4a892c707..7b17fa409 100644 --- a/public/assets/tests/model.js +++ b/public/assets/tests/model.js @@ -407,4 +407,35 @@ App.Delay.set( function() { 1400 ); +test("updateAttributes will change existing attributes and add new ones", function() { + App.Ticket.resetAttributes(); + + var attributesBefore = _.clone(App.Ticket.configure_attributes); + var updateAttribute = _.clone(attributesBefore[0]); + + updateAttribute['new_option_1239393'] = 1; + + App.Ticket.updateAttributes([ + updateAttribute, + { + name: 'new_attribute_1010101', + display: 'New Attribute', + tag: 'input', + readonly: 1, + }, + ]); + + var attributesAfterUpdate = _.clone(App.Ticket.configure_attributes); + + equal(attributesAfterUpdate.length, attributesBefore.length + 1, 'new attributes list contains 1 more elements') + equal(attributesAfterUpdate[attributesAfterUpdate.length - 1]['name'], 'new_attribute_1010101', 'new attributes list contains the new element') + equal(attributesAfterUpdate[0]['new_option_1239393'], 1, 'first element of the new attributes got updated with the new option') + + App.Ticket.resetAttributes(); + var attributesAfterReset = _.clone(App.Ticket.configure_attributes); + + equal(attributesAfterReset.length, attributesBefore.length, 'new attributes list has the same elements after reset') + equal(attributesAfterReset[0]['new_option_1239393'], undefined, 'first element of the new attributes has no attribute new_option_1239393') +}); + }