From 3c48c4f1ae5733602d0d0472bfa15518f7e7f98f Mon Sep 17 00:00:00 2001 From: Kyse Date: Mon, 30 May 2016 21:48:22 -0400 Subject: [PATCH 1/2] sfNewArray: Change titleMapValue binding to prevent ngModel target array being cleared. Allows multiple checkboxes (array builder) controls to target the same ngModel target array. --- src/directives/newArray.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/directives/newArray.js b/src/directives/newArray.js index 7acdb99d6..deffdc33e 100644 --- a/src/directives/newArray.js +++ b/src/directives/newArray.js @@ -120,15 +120,12 @@ function(sel, sfPath, schemaForm) { if (vals && vals !== old) { var arr = getOrCreateModel(); - // Apparently the fastest way to clear an array, readable too. - // http://jsperf.com/array-destroy/32 - while (arr.length > 0) { - arr.pop(); - } - form.titleMap.forEach(function(item, index) { - if (vals[index]) { + form.titleMap.forEach(function (item, index) { + var arrIndex = arr.indexOf(item.value); + if (arrIndex === -1 && vals[index]) arr.push(item.value); - } + if (arrIndex !== -1 && !vals[index]) + arr.splice(arrIndex, 1); }); // Time to validate the rebuilt array. From 66e342e9de9491a4d81653de411abb4647a8c23a Mon Sep 17 00:00:00 2001 From: Kyse Date: Tue, 31 May 2016 02:13:07 -0400 Subject: [PATCH 2/2] sfNewArray: Adding test for new titleMapValues binding. --- test/directives/schema-form-test.js | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test/directives/schema-form-test.js b/test/directives/schema-form-test.js index a34553c86..975b2863b 100644 --- a/test/directives/schema-form-test.js +++ b/test/directives/schema-form-test.js @@ -817,6 +817,57 @@ describe('directive',function(){ }); }); + it('should not clear the model when using multiple checkboxes targeting the same model array', function () { + + inject(function ($compile, $rootScope) { + var scope = $rootScope.$new(); + scope.person = { + "names": ["foo"] + }; + + scope.schema = { + "type": "object", + "properties": { + "names": { + "type": "array", + "items": { + "type": "string", + "enum": ["foo", "bar"] + } + } + } + }; + + scope.form = [ + 'names', + 'names', + { key: "names", type: "checkboxes", titleMap: { 'foo': 'Foo', 'bar': 'Bar' } }, + { key: "names", type: "checkboxes", titleMap: { 'foo': 'Foo', 'bar': 'Bar' } } + ]; + + var tmpl = angular.element('
'); + + $compile(tmpl)(scope); + $rootScope.$apply(); + + var foo = tmpl.children().eq(0).find('input[type=checkbox]').eq(0); + var bar = tmpl.children().eq(3).find('input[type=checkbox]').eq(1); + + foo.prop('checked').should.be.true; + bar.prop('checked').should.be.false; + scope.person.names.length.should.be.equal(1); + scope.person.names.join(',').should.be.equal('foo'); + + bar.click() + scope.person.names.length.should.be.equal(2); + scope.person.names.join(',').should.be.equal('foo,bar'); + + foo.click(); + scope.person.names.length.should.be.equal(1); + scope.person.names.join(',').should.be.equal('bar'); + }); + }); + it('should use radio buttons when they are wanted',function(){ inject(function($compile,$rootScope){