Created
May 2, 2011 20:34
-
-
Save shazmoh/952303 to your computer and use it in GitHub Desktop.
1.6 dojo store custom response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, body { | |
font-family: Arial; | |
} | |
h3 { | |
background-color: #efefef; | |
border-bottom: #ccc; | |
margin-bottom: 2px; | |
padding: 3px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h3>Custom Rest Store Items</h3> | |
<div id="customRestItems"> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dojo.require("dojo.store.JsonRest"); | |
function renderDetails(item, container){ | |
dojo.create("div", {innerHTML: item.id + "|" + item.name}, container); | |
} | |
dojo.addOnLoad(function(){ | |
dojo.declare("CustomJsonRest", dojo.store.JsonRest, { | |
// default records per page count | |
defaultCount: 25, | |
query: function(query, options){ | |
// summary: | |
// Overriding the query method to customize the change in paging and | |
// sorting info | |
var headers = {Accept: "application/javascript, application/json"}; | |
query = query || {}; | |
options = options || {}; | |
query.pageSize = options.count || this.defaultCount; | |
query.pageIndex = options.start ? options.start + 1 : 1; | |
// options.sort is an array of sort definitions | |
if(dojo.isArray(options.sort)){ | |
var sortField = options.sort[0].attribute; | |
var sortOrder = (options.sort[0].descending) ? "" : "^"; | |
query.sort = sortField + sortOrder; | |
} | |
query = dojo.objectToQuery(query); | |
query = query ? "?" + query: ""; | |
var deferred = dojo.xhrGet({ | |
url: this.target + (query || ""), | |
handleAs: "json", | |
headers: headers | |
}); | |
// Created a new deferred object and resolving it once | |
// the XHR deferred resolves | |
// Is this right way to do? | |
var results = new dojo.Deferred(); | |
deferred.then(function(data){ | |
results.resolve(data.results); | |
}); | |
results.total = deferred.then(function(data){ | |
return data.totalResultSize; | |
}); | |
return dojo.store.util.QueryResults(results); | |
} | |
}); | |
restStore = new CustomJsonRest({target: "/gh/gist/response.json/952303"}); | |
restStore.query().forEach(function(item){ | |
console.log("item:", item); | |
renderDetails(item, dojo.byId("customRestItems")); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
totalResultSize: 40, | |
results: [ | |
{id: 1, name: "one"}, | |
{id: 2, name: "two"}, | |
{id: 3, name: "three"}, | |
{id: 4, name: "four"}, | |
{id: 5, name: "five"}, | |
{id: 6, name: "six"} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsfiddle.net/gh/gist/dojo/1.6/952303/