diff --git a/ngModelOptions.js b/ngModelOptions.js index 413be40..b74c7db 100644 --- a/ngModelOptions.js +++ b/ngModelOptions.js @@ -19,7 +19,7 @@ } }(window, function (angular) { - angular.module("modelOptions", []).directive("ngModelOptions", function () { + angular.module("modelOptions", []).directive("ngModelOptions", ['$timeout', function ($timeout) { return { priority: 1, restrict: "A", @@ -54,9 +54,7 @@ } - scope.$apply(function () { - ctrls[0].$setViewValue(value); - }); + ctrls[0].$setViewValue(value); } function getSelectOption() { @@ -93,7 +91,7 @@ } } - var timer, + var timeoutPromise, NG_OPTIONS_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/, tagType = elm[0].nodeName.toLowerCase() + (attr.type || ""), defaultEvents = [], @@ -126,8 +124,8 @@ if (options.updateOn.match(/default/i)) { elm.on(defaultEvents.join(" "), function (e) { var delay = options.debounce["default"] || options.debounce || 0; - clearTimeout(timer); - timer = setTimeout(function () { + $timeout.cancel(timeoutPromise); + timeoutPromise = $timeout(function () { apply(); }, delay); }); @@ -153,8 +151,8 @@ } elm.on(event, function (e) { - clearTimeout(timer); - timer = setTimeout(function () { + $timeout.cancel(timeoutPromise); + timeoutPromise = $timeout(function () { apply(); }, delay); }); @@ -162,6 +160,6 @@ } }; - }); + }]); -})); \ No newline at end of file +})); diff --git a/ngModelOptions.min.js b/ngModelOptions.min.js index 7a6c8f3..a7b09c7 100644 --- a/ngModelOptions.min.js +++ b/ngModelOptions.min.js @@ -1 +1 @@ -!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("angular")):"function"==typeof define&&define.amd?define(["angular"],b):b(a.angular)}(window,function(a){a.module("modelOptions",[]).directive("ngModelOptions",function(){return{priority:1,restrict:"A",require:["ngModel"],link:function(b,c,d,e){function f(){var a;switch(j){case"inputradio":a=d.ngValue?b.$eval(d.ngValue):c.val();break;case"inputcheckbox":a=c.prop("checked");break;case"select":a=g();break;default:a=c.val(),d.ngTrim&&"false"===d.ngTrim.toLowerCase()||(a=a.replace(/^\s+|\s+$/g,""))}b.$apply(function(){e[0].$setViewValue(a)})}function g(){if(!d.ngOptions)throw"Must have ng-options";var e,f=d.ngOptions.match(i),g=f[8],h=f[7],j=b.$eval(h),k=c.val();return g?a.forEach(j,function(a,b){a[g.replace(/^.*?\./,"")]==k&&(e=b)}):e=k,/.*\..*/.test(f[1])?b.$eval(f[1].replace(/^.*?\./,h+"["+e+"].")):j[e]}var h,i=/^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/,j=c[0].nodeName.toLowerCase()+(d.type||""),k=[],l={debounce:0,updateOn:"default"},m=a.extend(l,b.$eval(d.ngModelOptions)),n="object"==typeof m.debounce,o=m.updateOn.split(" ");k="inputtext"===j||"textarea"===j?["keydown","input","change"]:"inputradio"===j||"inputcheckbox"===j?["click"]:"select"===j?["change"]:["keydown","input","change"],a.forEach(k,function(a){try{c.off(a)}catch(b){}}),m.updateOn.match(/default/i)&&c.on(k.join(" "),function(){var a=m.debounce["default"]||m.debounce||0;clearTimeout(h),h=setTimeout(function(){f()},a)}),a.forEach(o,function(a){if("default"!==a){var b;b=n?m.debounce[a]:m.debounce;var d=b;"undefined"==typeof d&&(d=m.debounce["default"]||0),c.on(a,function(){clearTimeout(h),h=setTimeout(function(){f()},d)})}})}}})}); \ No newline at end of file +!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("angular")):"function"==typeof define&&define.amd?define(["angular"],b):b(a.angular)}(window,function(a){a.module("modelOptions",[]).directive("ngModelOptions",["$timeout",function(b){return{priority:1,restrict:"A",require:["ngModel"],link:function(c,d,e,f){function g(){var a;switch(k){case"inputradio":a=e.ngValue?c.$eval(e.ngValue):d.val();break;case"inputcheckbox":a=d.prop("checked");break;case"select":a=h();break;default:a=d.val(),e.ngTrim&&"false"===e.ngTrim.toLowerCase()||(a=a.replace(/^\s+|\s+$/g,""))}f[0].$setViewValue(a)}function h(){if(!e.ngOptions)throw"Must have ng-options";var b,f=e.ngOptions.match(j),g=f[8],h=f[7],i=c.$eval(h),k=d.val();return g?a.forEach(i,function(a,c){a[g.replace(/^.*?\./,"")]==k&&(b=c)}):b=k,/.*\..*/.test(f[1])?c.$eval(f[1].replace(/^.*?\./,h+"["+b+"].")):i[b]}var i,j=/^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/,k=d[0].nodeName.toLowerCase()+(e.type||""),l=[],m={debounce:0,updateOn:"default"},n=a.extend(m,c.$eval(e.ngModelOptions)),o="object"==typeof n.debounce,p=n.updateOn.split(" ");l="inputtext"===k||"textarea"===k?["keydown","input","change"]:"inputradio"===k||"inputcheckbox"===k?["click"]:"select"===k?["change"]:["keydown","input","change"],a.forEach(l,function(a){try{d.off(a)}catch(a){}}),n.updateOn.match(/default/i)&&d.on(l.join(" "),function(a){var c=n.debounce.default||n.debounce||0;b.cancel(i),i=b(function(){g()},c)}),a.forEach(p,function(a){if("default"!==a){var c;c=o?n.debounce[a]:n.debounce;var e=c;"undefined"==typeof e&&(e=n.debounce.default||0),d.on(a,function(a){b.cancel(i),i=b(function(){g()},e)})}})}}}])}); \ No newline at end of file