Notes on Dojo: How to create a Data Store
Tested on: Dojo 1.7.2To create a Data Store first of all we have to fetch data from a source. For example, we can have a JSON file that contains data about books. The structure of the JSON file called bibliography.json is shown below.
[
{
"ID" : 1,
"Title" : "Book 1 Title",
"Publisher" : "Book 1 Publisher",
"City" : "Book 1 City",
"Authors" : "Author 1, Author 2",
"Year" : "2001"
},
...
]
To load this data we can use the Dojo XHR functionalities. Once data are fetched, we are able to create the store using, for instance, in memory data.
require(
[
"dojo",
"dojo/store/Memory"
],
function(dojo, MemoryStore){
var store = null;
var fetchData = dojo.xhrGet({ // returns a deferred object
url: "bibliography.json",
handleAs: "json",
error: function(error){
console.error("Something goes wrong: " + error.message);
}
});
fetchData.then(function(data){
store = new MemoryStore({data: data, idProperty: "ID"});
console.log(store); // print on console the store object
// query the store
var result = store.query({"Title": "Book 1 Title"});
console.log(result);
});
}
);
If all goes well, we will see on the console the retrieved information and the result of the query.
In this way we create a post that is coherent with Facebook, Linkedin and of course Twitter.
