From a40c3fbe08fb774ef1e2e6bbe0c00b797f1c71c2 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Tue, 26 Mar 2013 21:09:57 +0100 Subject: [PATCH] feat(typeahead): support the editable property Closes #269 --- src/typeahead/test/typeahead.spec.js | 12 ++++++++++++ src/typeahead/typeahead.js | 11 ++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index e444cf5790..8daeee9c75 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -271,6 +271,18 @@ describe('typeahead tests', function () { var matchHighlight = findMatches(element).find('a').html(); expect(matchHighlight).toEqual('prefixfoo'); }); + + it('should by default bind view value to model even if not part of matches', function () { + var element = prepareInputEl("
"); + changeInputValueTo(element, 'not in matches'); + expect($scope.result).toEqual('not in matches'); + }); + + it('should support the editable property to limit model bindings to matches only', function () { + var element = prepareInputEl("
"); + changeInputValueTo(element, 'not in matches'); + expect($scope.result).toEqual(undefined); + }); }); describe('selecting a match', function () { diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 3614f1b239..93e4ca7914 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -46,6 +46,9 @@ angular.module('ui.bootstrap.typeahead', []) //expressions used by typeahead var parserResult = typeaheadParser.parse(attrs.typeahead); + //should it restrict model values to the ones selected from the popup only? + var isEditable = originalScope.$eval(attrs.typeaheadEditable) !== false; + //create a child scope for the typeahead directive so we are not polluting original scope //with typeahead-specific data (matches, query etc.) var scope = originalScope.$new(); @@ -107,7 +110,7 @@ angular.module('ui.bootstrap.typeahead', []) } } - return undefined; + return isEditable ? inputValue : undefined; }); modelCtrl.$render = function () { @@ -151,13 +154,15 @@ angular.module('ui.bootstrap.typeahead', []) } else if (evt.which === 27) { evt.stopPropagation(); - scope.matches = []; + + resetMatches(); scope.$digest(); } }); $document.find('body').bind('click', function(){ - scope.matches = []; + + resetMatches(); scope.$digest(); });