Skip to content

Commit 6222e29

Browse files
committed
Merge pull request allenhwkim#210 from dlukez/event_arg_updates
fix(events): pass through updated model values
2 parents d0616c3 + c4d02d5 commit 6222e29

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

app/scripts/services/attr2_options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@
195195
var matches = attrValue.match(/([^\(]+)\(([^\)]*)\)/);
196196
var funcName = matches[1];
197197
var argsStr = matches[2].replace(/event[ ,]*/,''); //remove string 'event'
198-
199-
var args = scope.$eval("["+argsStr+"]");
198+
var argsExpr = $parse("["+argsStr+"]"); //for perf when triggering event
200199
return function(event) {
200+
var args = argsExpr(scope); //get args here to pass updated model values
201201
function index(obj,i) {return obj[i];}
202202
var f = funcName.split('.').reduce(index, scope);
203203
f && f.apply(this, [event].concat(args));

spec/services/attr2_options_spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ describe('Attr2Options', function() {
100100
var events = parser.getEvents(scope, attrs);
101101
expect(typeof events.click).toEqual('function');
102102
});
103+
it('should pass arguments to callback', function() {
104+
scope.name = 'dave';
105+
scope.scopeFunc = function() {}
106+
var attrs ={onClick:'scopeFunc(name)'};
107+
var events = parser.getEvents(scope, attrs);
108+
var event = {};
109+
spyOn(scope, 'scopeFunc');
110+
events.click(event);
111+
expect(scope.scopeFunc).toHaveBeenCalledWith(event, scope.name);
112+
});
113+
it('should respond to scope model changes', function() {
114+
scope.name = 'dave';
115+
scope.scopeFunc = function() {};
116+
var attrs ={onClick:'scopeFunc(name)'};
117+
var events = parser.getEvents(scope, attrs);
118+
var event;
119+
spyOn(scope, 'scopeFunc');
120+
scope.name = 'george';
121+
events.click(event);
122+
expect(scope.scopeFunc).toHaveBeenCalledWith(event, scope.name);
123+
});
103124
});
104125

105126
describe("#getAttrsToObserve", function() {

0 commit comments

Comments
 (0)