PDA

View Full Version : get the data model from a jsonview?



jbowman
3 Dec 2006, 6:49 PM
Is there any way to get to the jsonview data model? The jsonData propery is showing as undefined currently. I'm trying to create a few views from one json request, and right now my current idea is that since the jsonView is nice and sets up the datamodel so I can use names for the indexes, rather than index number, I'd like to

Create jsonView and "load" it. Create a contentPanel in my layout from that.
Get the datamodel, and create new view, assigning it the same datamodel, different jsonRoot.
Use that to add another contentPanel to my layout.


Here's the block of code I'm working with. Note: The alert I have in there is coming up "undefined".


var dh = YAHOO.ext.DomHelper;
dh.append(document.body, {tag: 'div', id: 'editUserDialog-user'});

var tpl = new YAHOO.ext.Template('<div>Username: {username}</div>' +
'<div>Birthday: {birthday}</div>' +
'<div>Member Since: {join_date}</div>' +
'<div>Last Login: {last_login}</div>');
tpl.compile();
var mainView = new YAHOO.ext.JsonView('editUserDialog-user',
tpl, {jsonRoot: 'user'});
mainView.load("/ci/admin/edit_user", "id=" + id);
alert(mainView.jsonData);
var eulayout = editUserDialog.getLayout();
eulayout.add('center', new YAHOO.ext.ContentPanel('editUserDialog-user', {title: 'Main Info'}));
editUserDialog.show();


Here's my JSON in it's current state (fields will eventually be the field data, not the model, but hey, still working on it here :))



{"user":[{"id":"7","username":"testcom","birthday":"1987-11-12","join_date":"2006-11-12","last_login":"2006-11-24","categories":[{"cat_name":"Basic Info","cat_id":"1","fields":[{"id":"1","name":"sex","cat":"1","type":"radio"}]},{"cat_name":"Interests and Personality","cat_id":"20","fields":[{"id":"3","name":"Self Description","cat":"20","type":"multi-line"}]}]}]}

jack.slocum
4 Dec 2006, 5:16 AM
If you want a data model, you'd have to use a YAHOO.ext.View. The JsonView works with UpdateManager, the default View works with a data model. If you still want to keep column names instead of indexes, see the docs for prepareData, override it and do your mapping.

On a side note, you can still do what you are looking to do with a JsonView, however you have to wait for the data to load. In your code the call to mainView.jsonData will always be undefined because load() is asynchronous.

The way you can listen for when the load completes:


mainView.el.getUpdateManager().on('update', function(){
alert(mainView.jsonData);
});

jbowman
4 Dec 2006, 6:22 AM
ok, I'll play around tonight. When i figure out the best way to load a multi-tab layoutDialog with one request for json data, I'll post an example :)