diff --git a/src/editors/extra/list.js b/src/editors/extra/list.js index 13520af3..32527ff4 100644 --- a/src/editors/extra/list.js +++ b/src/editors/extra/list.js @@ -83,7 +83,16 @@ this._checkMaxCardinalityReached($el); + //Save a copy of the pre-exising element, if exists + var domReferencedElement = this.el; + this.setElement($el); + + //In case of there was a pre-existing element already placed in the DOM, then update it + if (domReferencedElement) { + $(domReferencedElement).replaceWith(this.el); + } + this.$el.attr('id', this.id); this.$el.attr('name', this.key); @@ -224,6 +233,7 @@ }, setValue: function(value) { + this.items = []; this.value = value; this.render(); }, diff --git a/test/editors/extra/list.js b/test/editors/extra/list.js index b81c0a43..7e5fdab6 100644 --- a/test/editors/extra/list.js +++ b/test/editors/extra/list.js @@ -168,6 +168,26 @@ var same = deepEqual; same(list.getValue(), ['a', 'b', 'c']); }); + test('setValue() - updates input value - more than once', function() { + var list = new List().render(); + + list.setValue(['a', 'b', 'c']); + same(list.getValue(), ['a', 'b', 'c']); + + list.setValue(['d', 'e', 'f']); + same(list.getValue(), ['d', 'e', 'f']); + }); + + test('setValue() - add button works after calling setValue', function() { + var list = new List().render(); + + list.setValue(['a', 'b', 'c']); + + list.$('[data-action="add"]').click(); + + same(list.items.length, 4); + }); + test('validate() - returns validation errors', function() { var list = new List({ schema: { validators: ['required', 'email'] },