Tag Archive: JsonRestStore


Damn sometimes… Sometimes we have bugs that seem just impossible. Well I’ve just had one, I solved I just can’t resist posting it here. Guys I have to tell you it’s the most stupid bug I’ve sold, I laugh so much when I understood what happend. Ahhh life’s funny sometimes 😉

Here it is.

I’m querying a JsonRestStore that sends back this data :

[{“id”:”15″,”item_id”:”1″,”item_type”:”lunches”,”user_id”:”17″,”object”:”T”,”date”:”0000-00-00″,”child_of”:”0″,”TG”:””,”TD”:”Yes”,”TP”:””,”TA”:”Connect”,”TAD”:””,”D”:”0000-00-00″,”NC”:””},{“id”:”1″,”item_id”:”1″,”item_type”:”opportunities”,”user_id”:”17″,”object”:”T”,”date”:”2010-02-08″,”child_of”:”0″,”TG”:”re”,”TD”:”Yes”,”TP”:”2″,”TA”:”Older”,”TAD”:”v”,”D”:”0000-00-00″,”NC”:””},
{“id”:”3″,”item_id”:”1″,”item_type”:”opportunities”,”user_id”:”17″,”object”:”T”,”date”:”2010-02-08″,”child_of”:”0″,”TG”:”re”,”TD”:”Yes”,”TP”:”2″,”TA”:”Older”,”TAD”:”v”,”D”:”0000-00-00″,”NC”:””},
{“id”:”2″,”item_id”:”1″,”item_type”:”opportunities”,”user_id”:”17″,”object”:”T”,”date”:”2010-02-08″,”child_of”:”0″,”TG”:”re”,”TD”:”Yes”,”TP”:”2″,”TA”:”Older”,”TAD”:”v”,”D”:”0000-00-00″,”NC”:””}]

Well I removed the content of the data but that’s of no importance. Note that I had about 25 records sent from the JsonRestStore that looked pretty much the same except one field : item_type was for item 1 “lunches” and for all the rest “opportunities”.

Then my code, inside a dojo widget I’m fetching the JsonRestStore and getting the items. Except I want to order items using the field item_type.


// Fetch the objects
this.store.fetch({

query: {object:"Task"},
sort: sortKeys, // Order by... date DESC
onComplete: dojo.hitch(this, function(items,request){

opportunities="";
lunches="";

// Now format each element
for (var i = 0; i < items.length; i++){

var item=items[i];

if(item.item_type="lunches"){

lunches += '

';
//console.log(item.item_type + item.id);
}
else if (item.item_type="opportunities"){

lunches += '

';
}
}

// Add titles to the elements
if(lunches !=""){

lunches = "

Lunches

" + lunches ;
}
if(opportunities !=""){

opportunities = "

Opportunities

" + opportunities ;
}

this.TaskList.innerHTML = lunches + opportunities ;

dojo.parser.parse(this.domNode);

Guess what happend… All my items went on the lunches category. But WHY WHY WHY ???
You’ll find out I’m sure, and laugh a lot, hopefully 😉

Advertisements

I spend quiet a lot of time on this one, I posted arround here and there to find the anwser on how to make a JsonRestStore happend with Zend Framework. There are quiet a few tutorials out there but my main problem was a response from the server when POST request was made (when creating a new document).

Well here’s the solution :
$this->getResponse()
->setHttpResponseCode(201)
->setHeader('Content-Type', 'application/json', true)
->setHeader("Location", 'http://mywebsite' . $this->_request->getRequestUri() . $id);

$params['id'] = $id;
return $params;

First : you need to send back a code 201. Second, you need to send the url of the store with the new id (ex : mysite/store/4
Then you need to send back the content of the item created + the id in order to be able to play with it with DOJO. Otherwise, no fun.

Here’s the dojo part of the code :

testStore = new dojox.data.JsonRestStore({jsId:"tStore", target:"/test/", idAttribute:"id" });

// Get the content of a form with a magic dojo query
data = {};
dojo.query(" textarea, .ArticleNewFormElement > input").forEach(function(node, index, arr){
if(node.id){
data[node.id] = dijit.byId(node.id).attr("value");
}
});

// Add the item to the DataStore
var newClient = clientsStore.newItem(data);

// Save it back to the server
clientsStore.save({onComplete:function(item) {

// Now you can play with the ID of the element you just created, so cool !
console.log(newClient.id);

}
});

Note that I changed a few variables here and there for my blog without rechecking the entire stuff so re-read it if you need to use it, but it works. JsonRestStore is your friend I tell you !