我已经使用 Ext.Ajax.request() 实现了 Comet 解决方案,以便在发生超时或收到成功响应时重新初始化相同的请求。
var http = {
requestId: 0,
request: function(timeout){
Ext.Ajax.request({
url: '/echo/json/',
timeout: timeout || 10000,
method: 'POST',
scope: http, // scoped to the http object
params: {
json: Ext.encode({
data: [
{
id: 1,
type: 'notification',
timestamp: Ext.Date.format(new Date(), 'timestamp'),
from: 1445261,
to: 1402804,
read: false,
important: true,
content: 'Lorem ipsum...'
}
]
}),
delay: 5
},
success: function(resp) {
if (resp.status === 200){
console.log('success');
this.request();
}
},
failure: function(resp, opts) {
if (resp.timedout === true) {
console.log('failure');
this.request();
} else {
}
},
callback: function(options, success, resp) {
if (this.requestId === 0) {
this.requestId = resp.requestId;
}
}
});
}
};
http.request();
我想在 Ext JS MVC 中实现它并利用 native 代理从服务器获取数据并通过模型将其加载到商店中。
查看文档我看不出这是如何完成的,因为您似乎无法访问 Ext.Ajax.request 方法中的成功和失败回调。
有谁知道如何用Ext MVC架构实现长轮询?
上面的示例代码利用了 JSFiddle ajax JSON 响应回显:
最佳答案
我认为有 3 种方式:
一个更有趣、更简洁且可能更具挑战性的方法是扩展代理类以保持连接有效。有一个使用 Flicker API 的代理扩展示例 (sencha blog entry)。这可能会帮助您入门。
手动解析响应并根据响应数据创建模型对象,然后手动将它们插入到您的商店中。
使用商店回调方法永久加载商店,就像您处理 Ajax 请求一样。向下滚动到 store API 的动态加载部分有关使用回调函数加载商店的示例。还有一个
load
事件监听器,它会在读取数据后启动。
关于javascript - 使用 Comet 长轮询在 Ext JS 中更新模型和存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13657924/