Question

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

Was it helpful?

Solution

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.org/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. :)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top