Skip to content

Commit

Permalink
fix(directive): ng:options to support ng:change
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Jul 20, 2011
1 parent 0962b84 commit 89e9804
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Issue #449: [ng:options] should support binding to a property of an item.
- Issue #464: [ng:options] incorrectly re-grew options on datasource change
- Issue #448: [ng:options] should support iterating over objects
- Issue #463: [ng:options] should support firing ng:change event



Expand Down
6 changes: 5 additions & 1 deletion src/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ angularWidget('select', function(element){
this.directives(true);
var isMultiselect = element.attr('multiple');
var expression = element.attr('ng:options');
var onChange = expressionCompile(element.attr('ng:change') || "").fnSelf;
var match;
if (!expression) {
return inputWidgetSelector.call(this, element);
Expand Down Expand Up @@ -729,7 +730,10 @@ angularWidget('select', function(element){
value = valueFn(tempScope);
}
}
if (!isUndefined(value)) model.set(value);
if (!isUndefined(value) && model.get() !== value) {
onChange(scope);
model.set(value);
}
scope.$tryEval(function(){
scope.$root.$eval();
});
Expand Down
21 changes: 21 additions & 0 deletions test/widgetsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,27 @@ describe("widget", function(){
expect(scope.selected).toEqual(scope.values[1]);
});

it('should fire ng:change if present', function(){
createSelect({
name:'selected',
'ng:options':'value for value in values',
'ng:change':'count = count + 1'});
scope.values = [{name:'A'}, {name:'B'}];
scope.selected = scope.values[0];
scope.count = 0;
scope.$eval();
expect(scope.count).toEqual(0);

select.val('1');
browserTrigger(select, 'change');
expect(scope.count).toEqual(1);
expect(scope.selected).toEqual(scope.values[1]);

browserTrigger(select, 'change');
expect(scope.count).toEqual(1);
expect(scope.selected).toEqual(scope.values[1]);
});

it('should update model on change through expression', function(){
createSelect({name:'selected', 'ng:options':'item.id as item.name for item in values'});
scope.values = [{id:10, name:'A'}, {id:20, name:'B'}];
Expand Down

0 comments on commit 89e9804

Please sign in to comment.