jbowman
12-03-2006, 03:03 AM
I imagine this applies to the JSONDataModel actually.
I decided to start playing with JsonView to see if I could use it for templating, even just single instances. It works great. However, there is one thing I struggled with for a bit, and that's the format of the JSON returned. The elements you are trying to turn have to be arrays, even if it's a single instance.
For example, say you have the following code.
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("show_user.php", "id=" + id);
You might think that
{"user": {"username": "Bob", "birthday": "1976-11-08", "join_date": "2006-08-01", "last_login": "2006-12-03"}}
Would be a perfectly valid JSON response to your load to display within the template. Nope, it won't work.
The correct format for your JSON would be:
{"user": [{"username": "Bob", "birthday": "1976-11-08", "join_date": "2006-08-01", "last_login": "2006-12-03"}]}
Note the brackets to declare the user data is actually an array of user data. This is required to get the view to actually render based off of the data model.
Really, it makes perfect sense when you stop and think about what the purpose of a datamodel is for. However, since I struggled with this for a little bit, I didn't think it was too far out of reach others might also. So, here's the example and an explanation.
I decided to start playing with JsonView to see if I could use it for templating, even just single instances. It works great. However, there is one thing I struggled with for a bit, and that's the format of the JSON returned. The elements you are trying to turn have to be arrays, even if it's a single instance.
For example, say you have the following code.
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("show_user.php", "id=" + id);
You might think that
{"user": {"username": "Bob", "birthday": "1976-11-08", "join_date": "2006-08-01", "last_login": "2006-12-03"}}
Would be a perfectly valid JSON response to your load to display within the template. Nope, it won't work.
The correct format for your JSON would be:
{"user": [{"username": "Bob", "birthday": "1976-11-08", "join_date": "2006-08-01", "last_login": "2006-12-03"}]}
Note the brackets to declare the user data is actually an array of user data. This is required to get the view to actually render based off of the data model.
Really, it makes perfect sense when you stop and think about what the purpose of a datamodel is for. However, since I struggled with this for a little bit, I didn't think it was too far out of reach others might also. So, here's the example and an explanation.