tmpvar / weld

Template antimatter for Node.js (Browsers too!)
674 stars 39 forks source link

How to weld <a href="/thepath" class ="mylink">hello</a> #32

Closed Marak closed 13 years ago

Marak commented 13 years ago

What's the best practice for welding links? I really don't want to use string concatenation, anywhere.

I'd to generate: 'hello' from JSON.

Marak commented 13 years ago

To give you some perspective, here is my current approach, I'm delegating in the map based on the tagName.

    weld(dom, data, {
      map: function(parent, element, key, val) {
        if(element.tagName === 'A') {
          $(element).attr('href', val);
          $(element).html(path.basename(val));
          return false
        }
        element.innerHTML = val;
        return false;
      }
    });

This feels a little wrong to me. If I'm in a large weld my delegation logic could start getting a little complex? Also, it would be nice if things like links and form inputs were a little more friendly to weld to. I've done a bit of work that might be useful here: https://github.com/flatiron/jsonForm/tree/master/lib/jsonForm/plugins

heapwolf commented 13 years ago

i believe the following will work.

  weld(dom, data, {
    alias: {
      'a': function(parent, element, key, val) {
        element.href = val;
      }
    }
  });
Marak commented 13 years ago

Ahh! alias, will try that next time. Thank you.