How to create a Dojo store from JSON response

1.3k Views Asked by At

I am new to Dojo Frame work, so please do bear with me. I have a service implemented in a way that it will return JSON response . I am using Dojo frame work for UI. I am not sure about the right way to request to server and get the response in dojo.

I found 3 ways to request to server and receiving response.am not sure whether it's wrong or even remotely right

1)

request(contextName+"/service/getquestions/projectId/"+projectId,{
                    handleAs: "json"
                    }).then(function(data){alert("something")});

2)

var questionAnswerStore = new JsonRest({
                        target: contextName+"/service/getquestions/projectId/"+projectId,
                                       });

3)

request.get("contextName+"/service/getquestions/projectId/"+projectId",{
    handleAs: "json"
}).then(function(data){
    dataStore = new ObjectStore({ objectStore:new Memory({ data: data.items }) });
        });

Further, the store created will be used to populate the dgrid elements. Any help is appreciated.

2

There are 2 best solutions below

3
On

When working with widgets you should wrap your data in a dojo/store. That leaves out your first method.

The second one, like @PaulR told you, is the best approach. But it also means your REST service should follow certain rules which you can find at their reference guide. If you cannot meet these standards, you can choose to extend the dojo/store/JsonRest module so you can make it conform or you use the third method.

The third method is probably the easiest if your REST service is not compliant with the requirements. You do not need to use the dojo/data/ObjectStore adapter in the new versions of Dojo, since all widget stores should be using the dojo/store API now (and not the old dojo/data API).

4
On

The second way is the "best" one; Dojo is modeled on the Store interface for retrieving and manipulating (server-side) data. The other ways may not be wrong, but are not the "Dojo way" of using a REST interface.

The target parameter should contain the rest endpoint (/service/getquestions/projectId). When you want to retrieve information for a specific project, you would call store.get(projectId) on the store which would then perform a GET request to /service/getquestions/projectId/projectid and return the appropriate data.

In the same way an update can be done by calling store.put(object), which will do a PUT request on /service/getquestions/projectId/projectid.

Hope this helps. If not, more information on stores can be found here.