PDA

View Full Version : Data Store & Records



warlord0
13 Jul 2007, 8:45 AM
Ok, I'm missing something really fundamental I think. I just can't seem to get the access to data working right when you want to use it directly.

I'm using a bit of authentication using group memberships. This uses Ajax to query the server to see what groups a user is a member of.



function getMembership(userid) {
dsMembership = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: 'jsonAuth.php' //?query=getmembership&userid='+userid
}),
reader: new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total'
}, [
{success: 'success'},
{groups: 'groups'}
])
});
dsMembership.on('load', function() {
alert(dsMembership.getAt(0).results.success); // Response is "undefined" getCount yeilds 0 (Zero)
});
dsMembership.load({params:{query:'getmembership', userid:userid}});
}


Here is the json that get returned from my php script (as shown in firebug)



({"total":"2","results":{"success":true,"groups":["1","3"]}})


What I'm after is the array "groups" from the json data the rest is just filler to help identify that something was returned and how many.

Is my record definition correct to match the json data? Can anyone point out the error of my ways?

HeathT
1 Aug 2007, 5:49 PM
Did you find the answer to your question? I'm dealing with the same basic issue.

mystix
1 Aug 2007, 6:13 PM
try this reader definition instead


reader: new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total'
}, [
{name: 'success'},
{name: 'groups'}
])

gtomalesky
27 Sep 2007, 5:53 AM
I too have been getting data from a server. I am using the XMLReader. In Firebug, I can see the returned data in a Record array (this is in a 'load' event handler'). The returned data is displayed in the debugger window as belonging to the first(only) element of the Record array. I can see the exact data I am expecting but all attempts to extract from the Record object have failed (get('tag-value') or getAt() from Store). I have used this reader in support of grids and it works just fine there. Any suggestions

mihg
27 Sep 2007, 12:07 PM
Hey warlord0 I was also getting the same undefined errors and I think you need to wrap a your data with an index for each record; that's the only way I could get mine to work:



Ext.onReady(function(){
var ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'your.url.to.get.data.goes.here'}),
reader: new Ext.data.JsonReader({
root: 'results'
},['id','pp_poll_id', 'question', 'owner', 'owner_create_date'])
});

ds.load({callback:function(success){
if(success){
top.MainLayout.layout.el.unmask();
alert('count:'+ds.getCount());
alert(ds.getAt(0).get('id')); // you can grab your data like this by specifying the record index
ds.each(function(r){ //or like this by iterating through the records.
alert(r.get('id'));
})
}
}});


})


my JSON looks like this (firebug):



{"results":[{"id":"36","pp_poll_id":"31","question":"Who shot me?","owner":"Lincoln, Abraham","owner_create_date":"2007-09-26 14:45:01"}]}


my PHP array before I json_encode it looks like this (print_r()):



Array
(
[results] => Array
(
[0] => Array
(
[id] => 36
[pp_poll_id] => 31
[question] => need answer option here...
[owner] => Lincoln, Abraham
[owner_create_date] => 2007-09-26 14:45:01
)

)

)


-hope this helps