admin管理员组文章数量:1390571
I have the following code to implement Observable Memory Store
var inventory = [
{name:"shoes", quantity:10, category:"sales"},
{name:"clothes", quantity:5, category:"sales"},
{name:"hats", quantity:2, category:"sales"},
{name:"shirts", quantity:20, category:"sales"}
];
var inventoryStore = new Memory({data:inventory, idProperty: "name"});
var observer = new Observable(inventoryStore);
results = observer.query({});
results.observe(function(item, removedIndex, insertedIndex) {
if(removedIndex > -1) {
console.log("removed");
}
if(insertedIndex > -1) {
console.log("added");
}
console.log("Listened");
}, true);
inventoryStore.put(someObject);
Interestingly, the code does not listen to the changes made in inventoryStore
. I expected it to call observe()
method whenever something happens in the inventoryStore
but it does not. Instead, if I put object in the observer
not inventoryStore
then it listens.
If I change the code like the follow
var inventoryStore = new Observable(Memory({data:inventory, idProperty: "name"}));
results = inventoryStore.query({});
inventoryStore.put(someObject);
then it works. This is frustrating that even I followed exact code from the documentation and it does not work.
The reason why I have to use the first code block (putting object in inventoryStore
not in observer
) is that some of my object can't be stored in Observable Memory but only in Memory.
Any advice will be appreciated :)
I have the following code to implement Observable Memory Store
var inventory = [
{name:"shoes", quantity:10, category:"sales"},
{name:"clothes", quantity:5, category:"sales"},
{name:"hats", quantity:2, category:"sales"},
{name:"shirts", quantity:20, category:"sales"}
];
var inventoryStore = new Memory({data:inventory, idProperty: "name"});
var observer = new Observable(inventoryStore);
results = observer.query({});
results.observe(function(item, removedIndex, insertedIndex) {
if(removedIndex > -1) {
console.log("removed");
}
if(insertedIndex > -1) {
console.log("added");
}
console.log("Listened");
}, true);
inventoryStore.put(someObject);
Interestingly, the code does not listen to the changes made in inventoryStore
. I expected it to call observe()
method whenever something happens in the inventoryStore
but it does not. Instead, if I put object in the observer
not inventoryStore
then it listens.
If I change the code like the follow
var inventoryStore = new Observable(Memory({data:inventory, idProperty: "name"}));
results = inventoryStore.query({});
inventoryStore.put(someObject);
then it works. This is frustrating that even I followed exact code from the documentation and it does not work.
The reason why I have to use the first code block (putting object in inventoryStore
not in observer
) is that some of my object can't be stored in Observable Memory but only in Memory.
Any advice will be appreciated :)
2 Answers
Reset to default 7After hours of testing, it turns out that to observe changes in Memory Store
, you have to add / remove / update
objects through Observable object not through the Memory
.
This means you have two options to implement this
.
var inventoryStore = new Memory({data:inventory, idProperty:"name"});
var observer = new Observable(inventoryStore);
results = observer.query({});
observer.put(someObject);
or
var inventoryStore = new Observable(new Memory({data:inventory, idProperty:"name"});
results = inventoryStore.query({});
inventoryStore.put(someObject);
This may seem obvious but I was confused following the tutorial under this link.
http://www.tulek/2011/04/14/dojo-memory-and-observable-classes/
In addition,
observer.setData(another Inventory);
does not fire the observe()
method but just change data store in the observer. This causes mismatching
data store between Observable and Memory Store
since Memory Store still has the original inventory set.
The reason why some of my object couldn't be stored in Observable
was that I used dojo/calendar/Calendar
and it had a reference to some of the objects from the Memory that call some weird method due to property name mismatched.
I hope none of you people suffer from this matter. :)
Although this post is 2 years old, i really benefit from it, because i suffered the same problem. But my fault was to set the overwrite flag as second param when calling the observe method (facepalm).
So if any of you stick on this problem nevertheless, make sure to set the includeObjectUpdates param.
resultSet.observe(listener, includeObjectUpdates);
Bye.
本文标签: javascript(dojo) Observable cannot observe changes in Memory StoreStack Overflow
版权声明:本文标题:javascript - (dojo) Observable cannot observe changes in Memory Store - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744720012a2621639.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论