Frage

I've decided to give Dojo a shot instead of using JQuery for once, and am having trouble manipulating a data store. I've got a DataChart bound to the contents of an ItemFileWriteStore i've populated by hand from a web socket JSON message:

fakeData = {
        "identifier": "name",   
        "label": "Some data i'd like to add to later",
        "items": [
            {
                "name": "appendToMe",
                "values": [0.0, 1.0, 2.0, 3.0, 2.0, 1.0, 0.0]
            }
        ]
    };

store = new dojo.data.ItemFileWriteStore({
        data: fakeData
    });

var chart = new dojox.charting.DataChart("chartDiv", {});
chart.setStore(store, {"name":"*"}, "values");  

At this point, the graph is displaying the "appendToMe" series i've created. Next, I receive another message, containing some new numeric value for the "appendToMe" values list. How do I add it to the store, and will this be sufficient to trigger the graph to update?

I've looked at the [](write API) 'store.setValue', but it looks like I can only use this to replace the whole values chunk as one unit. In addition, I don't have a 'item' handle to use with the call, which appears to only be available if you use the newItem API instead of constructing the store with JSON.

Cheers!

Scott

War es hilfreich?

Lösung

First you need to fetch the appendToMe item.

store.fetchItemByIdentity({identity : 'appendToMe', 
                                      onItem : function (item) {
   var itemValues = store.getValues(item, 'values');
   itemValues.push(someNewValue);
   store.setValues(item, 'values', itemValues);
}});

Then in the onItem, get your values, modify them, and then set them again using store.setValue()

As noted, getValues needs to be used to return the array of values instead of the usual getValue, which never returns an array. And similar with setValues.

http://dojotoolkit.org/api/1.6/dojo/data/api/Read

This getValues() method works just like the getValue() method, but getValues() always returns an array rather than a single attribute value. The array may be empty, may contain a single attribute value, or may contain many attribute values. If the item does not have a value for the given attribute, then getValues() will return an empty array: []. (So, if store.hasAttribute(item, attribute) has a return of false, then store.getValues(item, attribute) will return [].)

setValues() http://dojotoolkit.org/api/1.6/dojo/data/api/Write

Adds each value in the values array as a value of the given attribute on the given item. Replaces any previous value or values. Calling store.setValues(x, y, []) (with values as an empty array) has the same effect as calling store.unsetAttribute(x, y).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top