Dojo how to get JSON attribute from dojo.data.Item

2019-06-24 01:46发布

问题:

I have the below JSON in the typeData variable that is then put into a dojo.data.ItemFileReadStore. What I need to know is how to check the value of status, was it set to "success" or some other value. I've not been able to figure out how to get the value of status from a ItemFileReadStore, any help would be greatly appreciated.

    var typesData = {
        status: "success",
        label: "name",
        identifier: "value",
        items: [
            {value: 3, name: "Truck"},
            {value: 8, name: "Van"},
            {value: 6, name: "Car"},
            {value: 7, name: "Scooter"}
        ]
    };
var test = new dojo.data.ItemFileReadStore({ data: typesData });

回答1:

The ItemFileReadStore will not handle additional attributes on the data object. However, you can extend the ItemFileReadStore to do what you need. You will be overriding 'internal' methods, so it's developer beware.

dojo.declare("MyCustomStore", [Store], {
    _getItemsFromLoadedData: function(/* Object */ dataObject){
        this.serverStatus = dataObject.status;                     
        this.inherited(arguments);                            
    }
});

var typesData = {
    status: "success",
    label: "name",
    identifier: "value",
    items: [
        {value: 3, name: "Truck"},
        {value: 8, name: "Van"},
        {value: 6, name: "Car"},
        {value: 7, name: "Scooter"}
    ]
};
var test = new MyCustomStore({ data: typesData });
test._forceLoad(); // forces the processing of the data object

console.debug(test.serverStatus);

http://jsfiddle.net/cswing/dVGSc/



标签: json dojo store