|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @ngdoc directive |
| 5 | + * @name ng.directive:ngUpdateModelOn |
| 6 | + * @restrict A |
| 7 | + * |
| 8 | + * @description |
| 9 | + * The `ngUpdateModelOn` directive changes default behavior of model updates. You can customize |
| 10 | + * which events will be bound to the `input` elements so that the model update will |
| 11 | + * only be triggered when they occur. |
| 12 | + * |
| 13 | + * This option will be applicable to those `input` elements that descend from the |
| 14 | + * element containing the directive. So, if you use `ngUpdateModelOn` on a `form` |
| 15 | + * element, the default behavior will be used on the `input` elements within. |
| 16 | + * |
| 17 | + * See {@link guide/forms this link} for more information about debouncing and custom |
| 18 | + * events. |
| 19 | + * |
| 20 | + * @element ANY |
| 21 | + * @param {string} ngUpdateModelOn Allows specifying an event or a comma-delimited list of events |
| 22 | + * that will trigger a model update. If it is not set, it defaults to any inmediate change. If |
| 23 | + * the list contains "default", the original behavior is also kept. You can also specify an |
| 24 | + * object in which the key is the event and the value the particular debouncing timeout to be |
| 25 | + * applied to it. |
| 26 | + */ |
| 27 | + |
| 28 | +var SIMPLEOBJECT_TEST = /^\s*?\{(.*)\}\s*?$/; |
| 29 | + |
| 30 | +var NgUpdateModelOnController = ['$attrs', '$scope', |
| 31 | + function UpdateModelOnController($attrs, $scope) { |
| 32 | + |
| 33 | + var attr = $attrs['ngUpdateModelOn']; |
| 34 | + var updateModelOnValue; |
| 35 | + var updateModelDebounceValue; |
| 36 | + |
| 37 | + if (SIMPLEOBJECT_TEST.test(attr)) { |
| 38 | + updateModelDebounceValue = $scope.$eval(attr); |
| 39 | + var keys = []; |
| 40 | + for(var k in updateModelDebounceValue) { |
| 41 | + keys.push(k); |
| 42 | + } |
| 43 | + updateModelOnValue = keys.join(','); |
| 44 | + } |
| 45 | + else { |
| 46 | + updateModelOnValue = attr; |
| 47 | + } |
| 48 | + |
| 49 | + this.$getEventList = function() { |
| 50 | + return updateModelOnValue; |
| 51 | + }; |
| 52 | + |
| 53 | + this.$getDebounceTimeout = function() { |
| 54 | + return updateModelDebounceValue; |
| 55 | + }; |
| 56 | +}]; |
| 57 | + |
| 58 | +var ngUpdateModelOnDirective = [function() { |
| 59 | + return { |
| 60 | + restrict: 'A', |
| 61 | + controller: NgUpdateModelOnController |
| 62 | + }; |
| 63 | +}]; |
| 64 | + |
| 65 | + |
| 66 | +/** |
| 67 | + * @ngdoc directive |
| 68 | + * @name ng.directive:ngUpdateModelDebounce |
| 69 | + * @restrict A |
| 70 | + * |
| 71 | + * @description |
| 72 | + * The `ngUpdateModelDebounce` directive allows specifying a debounced timeout to model updates so they |
| 73 | + * are not triggerer instantly but after the timer has expired. |
| 74 | + * |
| 75 | + * If you need to specify different timeouts for each event, you can use |
| 76 | + * {@link ng.directive:ngUpdateModelOn ngUpdateModelOn} directive which the object notation. |
| 77 | + * |
| 78 | + * @element ANY |
| 79 | + * @param {integer} ngUpdateModelDebounce Time in milliseconds to wait since the last registered |
| 80 | + * content change before triggering a model update. |
| 81 | + */ |
| 82 | +var NgUpdateModelDebounceController = ['$attrs', |
| 83 | + function UpdateModelDebounceController($attrs) { |
| 84 | + |
| 85 | + var updateModelDefaultTimeoutValue = $attrs['ngUpdateModelDebounce']; |
| 86 | + |
| 87 | + this.$getDefaultTimeout = function() { |
| 88 | + return updateModelDefaultTimeoutValue; |
| 89 | + }; |
| 90 | +}]; |
| 91 | + |
| 92 | +var ngUpdateModelDebounceDirective = [function() { |
| 93 | + return { |
| 94 | + restrict: 'A', |
| 95 | + controller: NgUpdateModelDebounceController |
| 96 | + }; |
| 97 | +}]; |
0 commit comments