Prototype - Template class and JSON

0 views
Skip to first unread message

addywaddy

unread,
Apr 20, 2007, 3:51:28 AM4/20/07
to Ruby on Rails: Spinoffs
Hi,

I'm wanting to use a Template for an active record object that has
been converted to json.

The resulting json is along these lines:

var group_with_admin = {"attributes" : {"name" : "Group 1"}, "users" :
{"attributes" : { "first" : "John"}}};

var groupTemplate = new Template('#{attributes.name} is administered
by #{users.attributes.first}');

groupTemplate.evaluate(group_with_admin); // -> is administered by

and if i change the template to ('#{attributes} is administered by
#{users}');

groupTemplate.evaluate(group_with_admin); // -> [object Object] is
administered by [object Object]

Obviously what I'm hoping for is to be able to render Group1 is
administered by John. Is there a way of doing this using the Template
class?

Regards

Adam

Christophe Porteneuve

unread,
Apr 20, 2007, 5:04:54 AM4/20/07
to rubyonrail...@googlegroups.com
Hi Adam,

addywaddy a écrit :


> var groupTemplate = new Template('#{attributes.name} is administered
> by #{users.attributes.first}');

Currently Template only uses direct properties of the evaluated object:
it doesn't go down. That's rather simple to fix, though. I'll consider
a patch soon about this, in the meantime here's a working fix:

evaluate: function(object) {
return this.template.gsub(this.pattern, function(match) {
var before = match[1];
if (before == '\\') return match[2];
// START OF FIX
var obj = object;
match[3].split('.').each(function(comp) {
if (obj)
obj = obj[comp];
});
return before + String.interpret(obj);
// END OF FIX
});
}


--
Christophe Porteneuve aka TDD
t...@tddsworld.com

addywaddy

unread,
Apr 20, 2007, 5:12:58 AM4/20/07
to Ruby on Rails: Spinoffs
A reply from the mighty Christophe!

Thanks for this mate. That'll make it child's play for updating the
page with an array of active record objects.

All the best

Adam

addywaddy

unread,
Apr 20, 2007, 8:41:07 AM4/20/07
to Ruby on Rails: Spinoffs
Christophe,

I mistyped my JSON:

var group_with_admin = {"attributes" : {"name" : "Group 1"}, "users" :

[{"attributes" : { "first" : "John"}}]};

users is an array. So I tried this:

var groupTemplate = new Template('#{attributes.name} is administered

by #{users[0].attributes.first}');

Thanks to your fix, #{attributes.name} renders "Group 1", but I can't
get to "John". My active record object is the result of this finder:

Group.find(:first, :include => :users)

where groups have many users.

I presume this isn't such a straight-forward fix. Any pointers? This
is the first time I've actually looked at the source code for
prototype - shame on me!

Regards

Adam

Christophe Porteneuve

unread,
Apr 20, 2007, 11:26:52 AM4/20/07
to rubyonrail...@googlegroups.com
Hey Andy,

addywaddy a écrit :


> Thanks to your fix, #{attributes.name} renders "Group 1", but I can't
> get to "John". My active record object is the result of this finder:

Yeah. I guess the fix can be made even more trivial by relying on the
String#evalJSON method in sanitized mode instead of manually running the
string.

Basically, this just requires prefixing each dynamic fragment with
"object." or something, then evalJSON'ing.

I don't have a minute to spare just now but you should try along these
lines, it probably goes something like this:

// START OF FIX
return before + ('object.' + match[3]).evalJSON(true);
// END OF FIX

Reply all
Reply to author
Forward
0 new messages