如何显示JsonRestStore中的数据

时间:2012-02-02 06:39:27

标签: javascript json rest dojo

以下是我编写的代码示例。我想从REST服务获取数据并将其打印在屏幕上。我可以用JSON格式从REST获得响应。但是,我找不到在JSONStore中使用它存储并使用它的方法。请帮我解决这个问题。

dojo.provide("index.IndexService");
dojo.require("dojo.parser"); 
dojo.require("dijit.Editor"); 
dojo.require("dijit.form.Button");
dojo.require("dijit.layout.BorderContainer"); 
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dojox.data.JsonRestStore");
var xhrArgs = {
        url: "http://localhost:8080/RestApp/service/customers",
        handleAs: "json",
        contentType : "application/json",
        load: function(data){
          alert(data);
        },
        error: function(error){
            alert(error);
        }
      }

dojo.ready(function(){
    // Create a button programmatically:
    var button = new dijit.form.Button({
        label: "View Transactions...",
        onClick: function(){
            // Do something:
            dojo.byId("result1").innerHTML += "Functionality yet to be implemented! ";
        }
    }, "progButtonNode");
    alert('hi');
    var store = new dojox.data.JsonRestStore({target: "http://localhost:8080/RestApp/service/customers"});

    alert(store);

    store.get(1).when(function(object){
        alert(object);
        // use the object with the identity of 3
      });



    //var deferred = dojo.xhrGet(xhrArgs);

    //compliantStore = new dojox.data.JsonRestStore({deferred});


    alert(deferred);
});

返回的JSON值是

  

{ “员工”:{ “客户”:[{ “城市”:空, “accountNo”:空, “名”:“名称   1" , “ID”:1},{ “城市”:空, “accountNo”:空, “名”:“名称   2" , “ID”:2}], “计数”:2}}

我将如何检索这些值?

1 个答案:

答案 0 :(得分:2)

JsonRestStore项目实际上是简单的JavaScript对象,因此您始终可以直接从项目中读取属性。像


var store = new dojox.data.JsonRestStore({target: "http://localhost:8080/RestApp/service/customers"});
myValue = recipeStore.getValue(item,"foo"); //stored in myValue
get = store.getValue;
set = store.setValue;
save = store.save;
// then fetch an item
var myGetValue = get(item,"foo");
var setMyValue = set(item,"foo","bar");

在同步模式下,可以通过直接从fetch操作返回的请求对象访问results属性来获取而不提供回调:


var queryResults = store.fetch({query:"?tastes='good'"}).results;
var firstItem = queryResults[0];

你有没有这样的意思..希望它有所帮助