我使用strophe.archive插件但是在Strophe发送查询后,Deferred对象的done函数似乎是“未定义的”。我还将插件添加到require.js的“垫片”中:
var q = $.Deferred();
q = connection.archive.listCollections("user@server.lit");
console.log(q);
q.done(function(){
console.log("DONE");
});
我得到以下追踪:
Uncaught TypeError: Cannot call method 'done' of undefined test.js:23
Backbone.View.extend.initialize test.js:23
Backbone.View backbone.js:1148
child backbone.js:1393
Backbone.Router.extend.pageTest router.js:92
(anonymous function) backbone.js:900
(anonymous function) backbone.js:1082
_.some._.any underscore.js:193
_.extend.loadUrl backbone.js:1080
_.extend.navigate backbone.js:1125
_.extend.navigate backbone.js:909
Backbone.View.extend.pageTest test1.js:199
jQuery.event.dispatch require-jquery.js:5385
elemData.handle.eventHandle
垫片:
archive: {
deps: ["jquery", "strophe"],
exports: "archive"
},
答案 0 :(得分:1)
您调用延迟的obj并将其分配给q
,然后将listCollections
的结果分配给q
。这是两个不同且完全不相关的任务。仅仅因为你最初提出q
并不意味着listCollections
的结果现在不知道deferrable
。
var q = $.Deferred(); // q is a deferred obj
q = connection.archive.listCollections("user@server.lit"); // q is undefined
listCollections
必须返回一个延迟对象,您才能在其上调用done
。
查看插件代码,看起来listCollections
没有返回任何内容。
相反,您必须传递listCollections
回调函数:
var q = $.Deferred();
q = connection.archive.listCollections("user@server.lit", null, function(){
console.log("DONE");
});
或者,您可以为listCollections
创建一个将返回延迟的包装函数。
function deferrableListCollections () {
var q = $.Deferred();
connection.archive.listCollections("user@server.lit", null, function(){
q.resolve() ;
});
return q;
}
不,您可以拨打类似于原始代码的内容:
q = deferrableListCollections("user@server.lit");
q.done(function(){
console.log("DONE");
});