-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Strange behavior of triggerHandler in jqLite #8469
Comments
I don't believe this is invalid behaviour. The event handler is (and should be) called with the I have not found any examples on the web that rely on the eventData being attached to the event as properties. Most of them don't use any parameters and just go to the triggering element to get the new file information. Can you provide information about why you expect it to behave as you suggest? |
Exactly my point. jQuery behavior and angular jqLite are not equal. When you call (angular.element(input)).triggerHandler("change", someDataObject) and jQuery(input).triggerHandler("change", someDataObject) jQuery returns this object as onChange argument while angular completely ignores it. Reason is as described in my first post concatenation between array and object. |
This Plunk shows that the first and only parameter sent to the handler when it is called for real is an This Plunk shows that jQuery's This Plunk shows how it should be done in jQuery. Note that instead of providing two parameters The real problem regarding Angular is that we are not supporting this ability to pass in our own |
In some scenarios you want to be able to specify properties on the event that is passed to the event handler. JQuery does this by overloading the first parameter (`eventName`). If it is an object with a `type` property then we assume that it must be a custom event. In this case the custom event must provide the `type` property which is the name of the event to be triggered. `triggerHandler` will continue to provide dummy default functions for `preventDefault()`, `isDefaultPrevented()` and `stopPropagation()` but you may override these with your own versions in your custom object if you wish. Closes angular#8469
In some scenarios you want to be able to specify properties on the event that is passed to the event handler. JQuery does this by overloading the first parameter (`eventName`). If it is an object with a `type` property then we assume that it must be a custom event. In this case the custom event must provide the `type` property which is the name of the event to be triggered. `triggerHandler` will continue to provide dummy default functions for `preventDefault()`, `isDefaultPrevented()` and `stopPropagation()` but you may override these with your own versions in your custom object if you wish. Closes angular#8469
In some scenarios you want to be able to specify properties on the event that is passed to the event handler. JQuery does this by overloading the first parameter (`eventName`). If it is an object with a `type` property then we assume that it must be a custom event. In this case the custom event must provide the `type` property which is the name of the event to be triggered. `triggerHandler` will continue to provide dummy default functions for `preventDefault()`, `isDefaultPrevented()` and `stopPropagation()` but you may override these with your own versions in your custom object if you wish. Closes angular#8469
In some scenarios you want to be able to specify properties on the event that is passed to the event handler. JQuery does this by overloading the first parameter (`eventName`). If it is an object with a `type` property then we assume that it must be a custom event. In this case the custom event must provide the `type` property which is the name of the event to be triggered. `triggerHandler` will continue to provide dummy default functions for `preventDefault()`, `isDefaultPrevented()` and `stopPropagation()` but you may override these with your own versions in your custom object if you wish. Closes angular#8469
In some scenarios you want to be able to specify properties on the event that is passed to the event handler. JQuery does this by overloading the first parameter (`eventName`). If it is an object with a `type` property then we assume that it must be a custom event. In this case the custom event must provide the `type` property which is the name of the event to be triggered. `triggerHandler` will continue to provide dummy default functions for `preventDefault()`, `isDefaultPrevented()` and `stopPropagation()` but you may override these with your own versions in your custom object if you wish. In addition the commit provides some performance and memory usage improvements by only creating objects and doing work that is necessary. This commit also renames the parameters inline with jQuery. Closes #8469 Closes #8505
FYI you can write: el.triggerHandler({
type: 'change',
target: {
files: [
new Blob(['data0']),
new Blob(['data1'])
]
}
});
el.triggerHandler(jQuery.Event('change', {
target: {
files: [
new Blob(['data0']),
new Blob(['data1'])
]
}
}));
el.triggerHandler($.Event('change', {
target: {
files: [
new Blob(['data0']),
new Blob(['data1'])
]
}
})); They are all similar. |
When using input file onchange functionality we can do it directly by binding function to change event:
(angular.element(input)).on("change", function (evt) {
//...use here evt.target.files
});
input.click();
Fun starts when I try to unit test it. Normally triggerHandler passes some dummy data (preventDefault and stopPropagation properties to be exact). In order to test it I need to pass some argument like this:
(angular.element(input)).triggerHandler("change", [{target: {files: fileList}]);
However triggerHandler passes in that case following data to my listenerFunction:
[{preventDefault: noop, stopPropagation: noop },{target: {files: fileList}]. Since actual image data is in second argument, listener will not pick it up.
I think error is in following line in angular.js in triggerHandler definition:
triggerHandler: function (element, eventName, eventData) {
...
eventData = eventData || [];
var event = [{
preventDefault: noop,
stopPropagation: noop
}]
forEach(eventFns, function (fn) {
fn.apply(element, event.concat(eventData));
});
}
which should be:
triggerHandler: function (element, eventName, eventData) {
...
var event = {
preventDefault: noop,
stopPropagation: noop
}
forEach(eventFns, function (fn) {
fn.apply(element, [eventData || event]);
});
}
and the call would then be:
(angular.element(input)).triggerHandler("change", {target: {files: fileList});
The text was updated successfully, but these errors were encountered: