← Back to team overview

launchpad-dev team mailing list archive

Re: 'stache, handlebars, tal and so forth

 

* Richard Harding <rick.harding@xxxxxxxxxxxxx> [2012-01-30 08:26 -0500]:
> On Fri, 27 Jan 2012, Marc Tardif wrote:
[snip]
> > 1. Make two passes over the data, once to massage the data and another
> >    time to render it. In other words, first render the raw JSON to cooked
> >    JSON and then the cooked JSON to HTML. I'm not so much concerned about
> >    the performance implication of rendering the same data twice as much
> >    as the nuisance of duplicating what is essentially display logic in
> >    two places.
> 
> There are a couple of ways this might go. Mustache does support
> "functions" or lambda depending on which docs you read. So you could add a
> function that does some date parsing logic to your objects as you pass them
> to the template and that function would accept the date string coming from
> the JSON and then try to pretty it up.

My understanding of the mustache.js documentation is that functions are
indeed supported but they are assumed to be contained in the JSON data
passed to the renderer. In other words, lets say I received something
like this from the API:

  var view = {
    "total_size": 4,
    "start": 0,
    "entries": [{
      "name": "ubuntuone-windows-installer",
      "test_run_count": 1,
      "development_focus_link": "http://results-tracker.ubuntu.com/+api/1.0/ubuntuone-windows-installer/trunk";,
      "http_etag": "\"e39ebbba23b4309381688f61a84fe17c2842999b\"",
      "self_link": "http://results-tracker.ubuntu.com/+api/1.0/ubuntuone-windows-installer";,
      "series_collection_link": "http://results-tracker.ubuntu.com/+api/1.0/ubuntuone-windows-installer/series";,
      "resource_type_link": "http://results-tracker.ubuntu.com/+api/1.0/#project";,
      "last_test_run_date": "2012-01-23T17:07:35.123315+00:00"}]};

To render the last_test_run_date properly, I would have to cook the view
to contain somekind of rendering function like this:

  for (entry in view.entries) {
    entry.date = function() {
      return function(text, render) {
        var formatter = new Y.lpresults.ui.DateFormatter({
            date: text
            });
        return formatter.displayDate();
      };
    };
  }

Then, I could have something like this in my template:

  {{#entries}}
    {{#date}}{{last_test_run_date}}{{/date}}
  {{/entries}}

If my understanding is correct, I'm rather uncomfortable with the idea of
having to cook the raw data like this. Otherwise, please let me know how
else this problem might be solved with mustache.js.

-- 
Marc Tardif <marc.tardif@xxxxxxxxxxxxx>
Freenode: cr3, Jabber: cr3@xxxxxxxxxx
1024D/72679CAD 09A9 D871 F7C4 A18F AC08 674D 2B73 740C 7267 9CAD



Follow ups

References