我正在尝试重新填充Memory
商店Observable
,使用Socket
来监控更新。我认为我对Memory/Observable
方很好,但我只是没有在Socket
方面的某个地方。
初始数据进入商店,我能够观察Socket
轮询数据,当我手动编辑数据时,它将获取(用于测试)我可以在控制台中看到它的变化。
require(["dojo/dom", "dojo/dom-construct", "dojo/store/Memory", "dojo/store/Observable", "dojox/socket", "dojo/domReady!"], function (dom, domConstruct, Memory, Observable, Socket) {
// initial data - would come from server
var data = [
{"type": "foo", "unread": "2"},
{"type": "bar", "unread": "28"},
{"type": "baz", "unread": "19"}
];
store = new Memory({data: data});
store = Observable(store);
var abc = '';
// display initial data
store.query().forEach(function(obj){
abc += '<div class="num">' + obj.unread + '</div>';
});
domConstruct.place(abc, dom.byId('nums'));
// Create Socket instance
var socket = new Socket.LongPoll({
url:"/path_to_my/endpoint/", // my endpoint in here returns a json object
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
}
});
socket.on("message", function(event){
var data = event.data;
console.log(event); // i can observe updated data here
// this is just copied from some tutorial page
// i'm not getting how this updates the store....
store.notify(data.action, {target: data.object, id: data.type});
});
});
See this JSFiddle, for a hopefully reasonable demonstration store.notify
位是我被解开的地方......
我还在控制台中注意到Socket
每秒轮询5个请求!我可以在这里申请一个油门,或者这是另一个不对劲的迹象吗?