From e9cd802c930e343d5e5515019b1ef9d38b8ee90d Mon Sep 17 00:00:00 2001 From: TEHEK Firefox Date: Tue, 11 Oct 2011 22:37:00 +0000 Subject: [PATCH 1/3] fix(ng:options): compile null/blank option tag Fixes #562 --- src/Compiler.js | 10 +++++- src/widget/select.js | 15 +++++++-- test/widget/selectSpec.js | 67 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/Compiler.js b/src/Compiler.js index a355444be770..e9f2fbe88a43 100644 --- a/src/Compiler.js +++ b/src/Compiler.js @@ -33,7 +33,15 @@ Template.prototype = { paths = this.paths, length = paths.length; for (i = 0; i < length; i++) { - children[i].link(jqLite(childNodes[paths[i]]), childScope); + // sometimes `element` can be modified by one of the linker functions in `this.linkFns` + // and childNodes may be added or removed + // TODO: element structure needs to be re-evaluated if new children added + // if the childNode still exists + if (childNodes[paths[i]]) + children[i].link(jqLite(childNodes[paths[i]]), childScope); + else + // if child no longer available, delete path + delete paths[i]; } }, diff --git a/src/widget/select.js b/src/widget/select.js index 1687721cc331..1576951345c7 100644 --- a/src/widget/select.js +++ b/src/widget/select.js @@ -243,9 +243,12 @@ angularWidget('select', function(element){ // find existing special options forEach(selectElement.children(), function(option){ - if (option.value == '') + var opt; + if (option.value == '') { + opt = jqLite(option); // User is allowed to select the null. - nullOption = {label:jqLite(option).text(), id:''}; + nullOption = {label:opt.text(), id:'', bindTemplate: opt.attr('ng:bind-template')}; + } }); selectElement.html(''); // clear contents @@ -397,6 +400,14 @@ angularWidget('select', function(element){ .val(option.id) .attr('selected', option.selected) .text(option.label); + + // if it's a nullOption, compile it + if (option.id === '') { + if (option.bindTemplate) + element.attr('ng:bind-template', option.bindTemplate); + compile(element)(modelScope); + } + existingOptions.push(existingOption = { element: element, label: option.label, diff --git a/test/widget/selectSpec.js b/test/widget/selectSpec.js index ad9dab18d743..6c3840b7b59b 100644 --- a/test/widget/selectSpec.js +++ b/test/widget/selectSpec.js @@ -429,6 +429,73 @@ describe('select', function() { expect(select.find('option').length).toEqual(1); }); }); + + describe('blank option', function () { + + function createSelect(attrs, blank, unknown){ + var html = ''; + select = jqLite(html); + scope = compile(select); + } + + function createSingleSelect(blank, unknown){ + createSelect({ + 'ng:model':'selected', + 'ng:options':'value.name for value in values' + }, blank, unknown); + } + + it('should be compiled as template, be watched and updated', function () { + var option; + + createSingleSelect(''); + scope.blankTemplate = 'so blank'; + scope.values = [{name:'A'}]; + scope.$digest(); + + // check blank option is first and is compiled + expect(select.find('option').length == 2); + option = jqLite(select.find('option')[0]); + expect(option.val()).toBe(''); + expect(option.text()).toBe('blank is so blank'); + + // change blankTemplate and $digest + scope.blankTemplate = 'not so blank'; + scope.$digest(); + + // check blank option is first and is compiled + expect(select.find('option').length == 2); + option = jqLite(select.find('option')[0]); + expect(option.val()).toBe(''); + expect(option.text()).toBe('blank is not so blank'); + }); + + it('should be compiled from ng:bind-template attribute if given instead of text', function () { + var option; + + createSingleSelect(''); + scope.blankTemplate = 'so blank'; + scope.values = [{name:'A'}]; + scope.$digest(); + + // check blank option is first and is compiled + expect(select.find('option').length == 2); + option = jqLite(select.find('option')[0]); + expect(option.val()).toBe(''); + expect(option.text()).toBe('blank is so blank'); + }); + }); describe('on change', function() { it('should update model on change', function() { From f9144f0eb16b23af50ed7bb0051a28ab4194e7bc Mon Sep 17 00:00:00 2001 From: TEHEK Firefox Date: Wed, 12 Oct 2011 16:46:59 +0000 Subject: [PATCH 2/3] fix(ng:options): compile null/blank option tag Fixes #562 --- src/widget/select.js | 35 ++++++++++++++++++----------------- test/widget/selectSpec.js | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/widget/select.js b/src/widget/select.js index 1576951345c7..8d2ef2266c97 100644 --- a/src/widget/select.js +++ b/src/widget/select.js @@ -243,11 +243,12 @@ angularWidget('select', function(element){ // find existing special options forEach(selectElement.children(), function(option){ - var opt; if (option.value == '') { - opt = jqLite(option); // User is allowed to select the null. - nullOption = {label:opt.text(), id:'', bindTemplate: opt.attr('ng:bind-template')}; + // save