Hi there,

I'm still thinking about extending json-template so that given a
(browser) DOM node and a name, I can retrieve the value in the JSON
structure at that given point.

I was thinking json-template could provide a hook point in the
{.section .. } and {.repeated section} rendering procedure to insert
some extra text there. (perhaps also for {.end} and {.or}), and issue
other side effects.

To implement my function I could then make it do this for each
{.section} and {.repeated section}:

* generate a unique id.

* use this unique id as the key, use as the value the JSON context (a
list or dictionary), and stuff this in a dictionary somewhere. (the
dictionary would be an attribute on the template probably, and would
be re-generated each expansion, which implies another small hook pre
or post-expansion).

* insert the following instead of {.section} etc:

<div class="json-template-marker" style="display:none" id="<the_unique_id>" />

Now to implement my javascript lookup function I could write something
like this (say as a method to the template):

var get_json_value = function(node, name) {
  var node = node.previousSibling;
  while (node) {
      if (has_class(node, 'json-template-marker')) {
         return this._json_contexts[get_id(node)];
      }
      node = node.previousSibling;
  }
  node = node.parentNode;
  if (!node) {
     return null;
  }
  return get_json_value(node name);
}

The nice thing about this is that I'd not need to modify my templates
at all to enable this functionality, and no need for a key or repeat
nr (this could be used as a possible optimization to avoid some DOM
walking, but would be done so internally).

The hook would also be generally useful to help with debugging or
template development (you could display the json context for each
section in the rendered template as part of a comment, for instance).

So, what about such a hook? Any suggestions as where I'd need to start
hacking? (in the JS implementation to start with).

Regards,

Martijn

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JSON 
Template" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/json-template?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to