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 {
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
element.bind('keydown change input', function(event) {
var key = event.keyCode;
// command modifiers arrows
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) return;
$browser.defer(function() {
var touched = ctrl.touch(),
var handler = function() {
var touched = ctrl.touch(),
value = trim(element.val());
if (ctrl.viewValue !== value) {
@ -1055,8 +1049,24 @@ var ngModelInstantDirective = ['$browser', function($browser) {
} else if (touched) {
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');
browserTrigger(inputElm, 'change');
$browser.defer.flush();
expect(scope.value).toBe('value2');
if (msie < 9) return;
inputElm.val('value3');
browserTrigger(inputElm, 'input');
$browser.defer.flush();
expect(scope.value).toBe('value3');
}));
});