fix(ng:model-instant): defer only keydown, throttle setTimeouts

This commit is contained in:
Vojta Jina
2012-02-27 14:49:36 -08:00
parent e7d6106811
commit 4e83399570
2 changed files with 19 additions and 11 deletions

View File

@@ -1038,14 +1038,8 @@ var ngModelInstantDirective = ['$browser', function($browser) {
return { return {
require: 'ngModel', require: 'ngModel',
link: function(scope, element, attr, ctrl) { link: function(scope, element, attr, ctrl) {
element.bind('keydown change input', function(event) { var handler = function() {
var key = event.keyCode; var touched = ctrl.touch(),
// command modifiers arrows
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) return;
$browser.defer(function() {
var touched = ctrl.touch(),
value = trim(element.val()); value = trim(element.val());
if (ctrl.viewValue !== value) { if (ctrl.viewValue !== value) {
@@ -1055,8 +1049,24 @@ var ngModelInstantDirective = ['$browser', function($browser) {
} else if (touched) { } else if (touched) {
scope.$apply(); scope.$apply();
} }
}); };
var timeout;
element.bind('keydown', function(event) {
var key = event.keyCode;
// command modifiers arrows
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) return;
if (!timeout) {
timeout = $browser.defer(function() {
handler();
timeout = null;
});
}
}); });
element.bind('change input', handler);
} }
}; };
}]; }];

View File

@@ -949,14 +949,12 @@ describe('input', function() {
inputElm.val('value2'); inputElm.val('value2');
browserTrigger(inputElm, 'change'); browserTrigger(inputElm, 'change');
$browser.defer.flush();
expect(scope.value).toBe('value2'); expect(scope.value).toBe('value2');
if (msie < 9) return; if (msie < 9) return;
inputElm.val('value3'); inputElm.val('value3');
browserTrigger(inputElm, 'input'); browserTrigger(inputElm, 'input');
$browser.defer.flush();
expect(scope.value).toBe('value3'); expect(scope.value).toBe('value3');
})); }));
}); });