问题描述
我正在尝试获取jQuery 工作并遇到一些问题。
我的系统使用 postMessage
来传递消息来自和到网站上的沙箱区域。因此,当我从沙盒区域请求服务时:
//点击按钮
每次点击按钮并且服务是请求如下:
foo.requestService (选项,功能(响应){
$(#c)。val(响应); //设置输入按钮的值
}
that.requestService = $ .fn.requestService = function(options,callbackFunction){
var deferred = new $ .Deferred(),
callbackId = priv.generateUuid(),
callback = deferred;
//存储回调以由响应处理程序检索
priv.trackCallback(callbackId,callback,callbackFunction);
// set type
if(options.type === undefined){
options.type =request /任何;
}
//一个postMessage
//收到响应后,要运行的回调函数
deferred.done(function(result,callbackFunction){
if(callbackFunction){
callbackFunction(result);
}
});
//触发消息传递
window.top.postMessage(options,window.location.href);
};
一切正常,我可以请求我的沙盒服务,运行它,postMessage结果并检索相应的回调:
priv.returnResult = function(event){
//这里我得到了延迟回调[回调[ 0])和回调函数(callback [1])$ b $ b var callback = priv.retrieveCallback(event.data.callback);
// problem =第一个函数调用后回调保持解析
console.log(callback [0] .state());
callback [0] .resolve(event.data.result,callback [1]);
};
我的问题是,虽然我正在创建
**新** $ .Deferred
每次请求服务时,我的延迟对象仅在第一次运行,然后设置为已解决
,从而阻止任何进一步的函数调用返回结果。
我认为
new $ .Deferred
会在每次服务时产生新的延迟请求,我可以解决运行我的回调函数。
问题
我需要做些什么才能让它发挥作用?不是吗,新会创建新对象,我可以解决这个问题吗?我是否必须以某种方式删除/删除以前的resolved.deferred对象才能使其正常工作?
谢谢!
解决方案找到一个延迟jQuery延迟处理多个延迟设置器和解析器的片段:
$ .extend({
StatelessDeferred:function(){
var doneList = $ .Callbacks(memory),
promise = {
done:doneList.add,
//获得此延期
的承诺//如果提供了obj,则将promise方面添加到对象
promise:function(obj){
var i,
keys = ['done','promise'];
if(obj === undefined){
obj = promise;
} else {
for(i = 0; i< keys.length; i + = 1){
obj [keys [i]] = promise [keys [i ]];
}
}
返回obj;
}
},
deferred = promise.promise({});
deferred.resolveWith = doneList.fireWith;
deferred.resolve = doneList.fire;
//全部完成!
返回递延;
}
});
I'm trying to get jQuery
deferred
to work and am having some problems.My system is using
postMessage
to pass messages from and to sandboxed areas on a website. So when I request a service from a sandboxed area like this:// on click of button foo.requestService(options, function (response) { $("#c").val(response); // set value of input button }
Internally, I'm creating a
new $.deferred
every time the button is clicked and the service is requested like so:that.requestService = $.fn.requestService = function (options, callbackFunction) { var deferred = new $.Deferred(), callbackId = priv.generateUuid(), callback = deferred; // store callback to be retrieved by response handler priv.trackCallback(callbackId, callback, callbackFunction); // set type if (options.type === undefined) { options.type = "request/any"; } // the callback function to be run, once a postMessage // with the response is received deferred.done(function(result, callbackFunction) { if (callbackFunction) { callbackFunction(result); } }); // trigger messaging window.top.postMessage(options, window.location.href); };
All works well, I can request my sandboxed service, run it, postMessage the result and retrieve the respective callback:
priv.returnResult = function (event) { // here I get back the deferred (callback[0]) and callback function (callback[1]) var callback = priv.retrieveCallback(event.data.callback); // problem = the callback stays resolved after the first function call console.log(callback[0].state()); callback[0].resolve(event.data.result, callback[1]); };
My problem is, although I'm creating a
**new** $.Deferred
every time a service is requested, my deferred object only runs the first time and then is set toresolved
, preventing any further function calls to return a result.I thought a
new $.Deferred
would generate a new deferred every time a service is requested, which I can resolve to run my callback function.Question
What do I need to do to get this to work? Is it not, that new creates a new object, which I can resolve? Do I have to somehow delete/remove the previous resolved.deferred object in order to make it work?Thanks!
解决方案Found a snippet extending jQuery deferred to handle "multiple deferred setters and resolvers":
$.extend({ StatelessDeferred: function () { var doneList = $.Callbacks("memory"), promise = { done: doneList.add, // Get a promise for this deferred // If obj is provided, the promise aspect is added to the object promise: function (obj) { var i, keys = ['done', 'promise']; if (obj === undefined) { obj = promise; } else { for (i = 0; i < keys.length; i += 1) { obj[keys[i]] = promise[keys[i]]; } } return obj; } }, deferred = promise.promise({}); deferred.resolveWith = doneList.fireWith; deferred.resolve = doneList.fire; // All done! return deferred; } });
这篇关于为什么在调用多次设置新$ .Deferred的函数时,我的延迟对象设置为已解决而不是挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!