fix(ngmodel): fixing many keys incorrectly marking inputs as dirty

This commit is contained in:
Jason Bedard
2014-10-28 01:25:45 -07:00
committed by Caitlin Potter
parent 55d9db56a6
commit d21dff21ed
2 changed files with 30 additions and 5 deletions

View File

@@ -973,6 +973,10 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}
var listener = function(ev) {
if (timeout) {
$browser.defer.cancel(timeout);
timeout = null;
}
if (composing) return;
var value = element.val(),
event = ev && ev.type;
@@ -999,11 +1003,13 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
} else {
var timeout;
var deferListener = function(ev) {
var deferListener = function(ev, input, origValue) {
if (!timeout) {
timeout = $browser.defer(function() {
listener(ev);
timeout = null;
if (!input || input.value !== origValue) {
listener(ev);
}
});
}
};
@@ -1015,7 +1021,7 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
// command modifiers arrows
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) return;
deferListener(event);
deferListener(event, this, this.value);
});
// if user modifies input value using context menu in IE, we need "paste" and "cut" events to catch it

View File

@@ -1804,7 +1804,7 @@ describe('input', function() {
}
});
describe('"paste" and "cut" events', function() {
describe('"keydown", "paste" and "cut" events', function() {
beforeEach(function() {
// Force browser to report a lack of an 'input' event
$sniffer.hasEvent = function(eventName) {
@@ -1812,9 +1812,13 @@ describe('input', function() {
};
});
it('should update the model on "paste" event', function() {
it('should update the model on "paste" event if the input value changes', function() {
compileInput('<input type="text" ng-model="name" name="alias" ng-change="change()" />');
browserTrigger(inputElm, 'keydown');
$browser.defer.flush();
expect(inputElm).toBePristine();
inputElm.val('mark');
browserTrigger(inputElm, 'paste');
$browser.defer.flush();
@@ -1830,6 +1834,21 @@ describe('input', function() {
expect(scope.name).toEqual('john');
});
it('should cancel the delayed dirty if a change occurs', function() {
compileInput('<input type="text" ng-model="name" />');
var ctrl = inputElm.controller('ngModel');
browserTrigger(inputElm, 'keydown', {target: inputElm[0]});
inputElm.val('f');
browserTrigger(inputElm, 'change');
expect(inputElm).toBeDirty();
ctrl.$setPristine();
scope.$apply();
$browser.defer.flush();
expect(inputElm).toBePristine();
});
});