Skip to content

Commit b225083

Browse files
committed
Fire $eval only if the value has actually changed on input
1 parent e84d333 commit b225083

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/widgets.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ function optionsAccessor(scope, element) {
373373

374374
function noopAccessor() { return { get: noop, set: noop }; }
375375

376-
var textWidget = inputWidget('keyup change', modelAccessor, valueAccessor, initWidgetValue()),
376+
var textWidget = inputWidget('keyup change', modelAccessor, valueAccessor, initWidgetValue(), true),
377377
buttonWidget = inputWidget('click', noopAccessor, noopAccessor, noop),
378378
INPUT_TYPE = {
379379
'text': textWidget,
@@ -451,7 +451,7 @@ function radioInit(model, view, element) {
451451
expect(binding('checkboxCount')).toBe('1');
452452
});
453453
*/
454-
function inputWidget(events, modelAccessor, viewAccessor, initFn) {
454+
function inputWidget(events, modelAccessor, viewAccessor, initFn, dirtyChecking) {
455455
return function(element) {
456456
var scope = this,
457457
model = modelAccessor(scope, element),
@@ -463,10 +463,13 @@ function inputWidget(events, modelAccessor, viewAccessor, initFn) {
463463
// Don't register a handler if we are a button (noopAccessor) and there is no action
464464
if (action || modelAccessor !== noopAccessor) {
465465
element.bind(events, function (){
466-
model.set(view.get());
467-
lastValue = model.get();
468-
scope.$tryEval(action, element);
469-
scope.$root.$eval();
466+
var value = view.get();
467+
if (!dirtyChecking || value != lastValue) {
468+
model.set(value);
469+
lastValue = model.get();
470+
scope.$tryEval(action, element);
471+
scope.$root.$eval();
472+
}
470473
});
471474
}
472475
scope.$watch(model.get, function(value){

test/widgetsSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ describe("widget", function(){
4242
expect(scope.$get('count')).toEqual(2);
4343
});
4444

45+
it('should not trigger eval if value does not change', function(){
46+
compile('<input type="Text" name="name" value="Misko" ng:change="count = count + 1" ng:init="count=0"/>');
47+
expect(scope.name).toEqual("Misko");
48+
expect(scope.count).toEqual(0);
49+
browserTrigger(element, 'keyup');
50+
expect(scope.name).toEqual("Misko");
51+
expect(scope.count).toEqual(0);
52+
});
53+
4554
it('should allow complex refernce binding', function(){
4655
compile('<div ng:init="obj={abc:{}}">'+
4756
'<input type="Text" name="obj[\'abc\'].name" value="Misko""/>'+

0 commit comments

Comments
 (0)