最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - (dojo) Observable cannot observe changes in Memory Store - Stack Overflow

programmeradmin1浏览0评论

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 :)

Share Improve this question edited Aug 12, 2013 at 2:51 Eugene Yu asked Aug 12, 2013 at 2:28 Eugene YuEugene Yu 3,9884 gold badges23 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

After 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.

发布评论

评论列表(0)

  1. 暂无评论