How can i achieve this?
$(window).bind('done', function (e) {
e.result = "my data";
});
var state = $(window).trigger('done');
equal("my data", state);
Currently .trigger
returns the JQuery object.
UPDATE
Event is triggered from another module, so i can't use closure开发者_Python百科s.
You have to appreciate that event handlers are asynchronous in JavaScript. This implies that a call to an asynchronous function has no output, no return value. The only way to get data from an asynchronous function is by passing in a callback that will be called whenever the output is available - or when the event that you're waiting on has occurred.
jQuery has a rather elegant deferred
API for this. I'm not how it works together with event handlers as I don't use jQuery very much but I guess that is what you should be going for, rather than this .. abomination.
Frits is correct -- an event handler cannot return a value. You can think of it like an anonymous tip-off to the police: The caller leaves some information with the police, the police do something with that information. The caller may have triggered the police response, but the police don't know where the information came from and the caller has no influence on how his information is used.
That said, you can pretty easily do what you're trying to do here, you just need to rearrange things a bit...
var state = null;
$(window).bind('done', function (e) {
state = "my data";
equal("my data", state);
});
$(window).trigger('done');
I am also looking for this. Although in principle, it is wrong to get the data from the handler. Anyway, the following is the idea
No, you can't get the data from the handler. because of the following https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.dispatchEvent
But if you are using JQuery, (it is most likely)
Since the return value will be stored in the event object as event.result in JQuery and this event object is passed through all the handlers, so in the handlers you are able to get the previous handlers result through this 'event.result'
How about the original event trigger?
Jquery will wrap the original event into another event object by the following code
// Make a writable jQuery.Event from the native event object event = jQuery.event.fix( event );
The original event is stored as the event.originalEvent. If you do need pass some information back to the original event trigger function. The solution is just put the value into that object's attribute
event.originalEvent.xxxx = xxxx;
Had to move portion of JS code to be modular (move to separate file that could be plugged in), in one place knockout.js list had to be reset if module is used.
so this code:
var treeSettings = eval(knockoutContext).Tree;
if (oldData === listArray) {
oldData = options.isObservable && !(treeSettings && treeSettings.IncludeChildren && treeSettings.IsBeingReconstructed) ? listArray().slice(0, rows.length) : [];
listArray(oldData);
}
became this:
if (oldData === listArray) {
oldData = options.isObservable ? listArray().slice(0, rows.length) : [];
$(document).trigger('OnTheMove_OfflineJSBeforeListArrayReused', {knockoutContext : knockoutContext, callback : function(){ oldData = [];}});
listArray(oldData);
}
and in separate file.
$(document).on('OnTheMove_OfflineJSBeforeListArrayReused', function (e, options) {
var treeSettings = eval(options.knockoutContext).Tree;
if (treeSettings && treeSettings.IncludeChildren && treeSettings.IsBeingReconstructed) {
options.callback();
}
});
I know handler and callback are async still seems to work fine.
精彩评论