From 7802c90e139a36d37b9d3c8cd6b6fcfee042dd71 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 14 Jul 2011 13:27:39 -0700 Subject: fix(directive): ng:options to support iterating over objects Closes #448 --- src/widgets.js | 61 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'src/widgets.js') diff --git a/src/widgets.js b/src/widgets.js index f8ada667..bdd68804 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -596,14 +596,23 @@ angularWidget('button', inputWidgetSelector); * * binding to a value not in list confuses most browsers. * * @element select - * @param {comprehension_expression} comprehension _select_ `as` _label_ `for` _item_ `in` _array_. + * @param {comprehension_expression} comprehension in following form * - * * _array_: an expression which evaluates to an array of objects to bind. - * * _item_: local variable which will refer to the item in the _array_ during the iteration + * * _select_ `for` _value_ `in` _array_ + * * _select_ `as` _label_ `for` _value_ `in` _array_ + * * _select_ `for` `(`_key_`,` _value_`)` `in` _object_ + * * _select_ `as` _label_ `for` `(`_key_`,` _value_`)` `in` _object_ + * + * Where: + * + * * _array_ / _object_: an expression which evaluates to an array / object to iterate over. + * * _value_: local variable which will reffer to the item in the _array_ or _object_ during + * iteration + * * _key_: local variable which will refer to the key in the _object_ during the iteration * * _select_: The result of this expression will be assigned to the scope. * The _select_ can be ommited, in which case the _item_ itself will be assigned. * * _label_: The result of this expression will be the `option` label. The - * `expression` most likely reffers to the _item_ variable. (optional) + * `expression` most likely refers to the _item_ variable. (optional) * * @example @@ -658,8 +667,8 @@ angularWidget('button', inputWidgetSelector); */ - -var NG_OPTIONS_REGEXP = /^\s*((.*)\s+as\s+)?(.*)\s+for\s+([\$\w][\$\w\d]*)\s+in\s+(.*)$/; +// 000012222111111111133330000000004555555555555555554666666777777777777777776666666888888888888888888888864000000009999 +var NG_OPTIONS_REGEXP = /^\s*((.*)\s+as\s+)?(.*)\s+for\s+(([\$\w][\$\w\d]*)|(\(\s*([\$\w][\$\w\d]*)\s*,\s*([\$\w][\$\w\d]*)\s*\)))\s+in\s+(.*)$/; angularWidget('select', function(element){ this.descend(true); this.directives(true); @@ -671,13 +680,14 @@ angularWidget('select', function(element){ } if (! (match = expression.match(NG_OPTIONS_REGEXP))) { throw Error( - "Expected ng:options in form of '(_expression_ as)? _expresion_ for _item_ in _collection_' but got '" + + "Expected ng:options in form of '_select_ (as _label_)? for (_key_,)?_value_ in _collection_' but got '" + expression + "'."); } var displayFn = expressionCompile(match[3]).fnSelf; - var itemName = match[4]; - var itemFn = expressionCompile(match[2] || itemName).fnSelf; - var collectionFn = expressionCompile(match[5]).fnSelf; + var valueName = match[5] || match[8]; + var keyName = match[7]; + var valueFn = expressionCompile(match[2] || valueName).fnSelf; + var valuesFn = expressionCompile(match[9]).fnSelf; // we can't just jqLite('