mixin attributes

153 views
Skip to first unread message

Patrick Lee

unread,
Mar 27, 2013, 11:01:13 PM3/27/13
to jad...@googlegroups.com
I didn't see any documentation on the attributes object in mixins.  Is changing the attributes value in mixins supported or is my list mixin a "hack" that works?

mixin list(obj)
  items = obj.items || []
  tag = obj.tag || 'ol'
  attr = obj.attr || {}
  childAttr = obj.childAttr || {}
  childTag = obj.childTag || 'li'

  attributes = attr

  #{tag}(attributes)
   
   attributes = childAttr
   
   for item in items
    #{childTag}(attributes)= item


+list({attr:{class: "parent", data: "p"}, items:[1,2,3], childAttr: {class:'child', data: "c"}})

Produces :

<ol class="parent" data="p">
  <li class="child" data="c">1</li>
  <li class="child" data="c">2</li>
  <li class="child" data="c">3</li>
</ol>


Thanks in advance.

greelgorke

unread,
Mar 28, 2013, 3:56:39 AM3/28/13
to jad...@googlegroups.com
it's a hack. if i'm correct, you're just overriding the attributes property of the scope in currently surrounding function. jade compiles the templates to js-functions. and you're do not really have to override it. just pass the attr and childAttr.

Patrick Lee

unread,
Mar 28, 2013, 9:59:21 AM3/28/13
to jad...@googlegroups.com
Thanks for the response.  How can I just pass in attr or childAttr?  The attributes operator takes a comma separated list of property=value.  

div({name: 'b'})
produces
<div name: b="[object Object]"></div>  

greelgorke

unread,
Mar 29, 2013, 4:18:27 PM3/29/13
to jad...@googlegroups.com
ah i see, it's tricky. one solution would be to create plain html. other one is to not to be so generic. i don't think  #{tag}(attributes) should work, i guess it's a bug anyway.

the not-so-generic solution might look like this:

  #{tag}(class='#{attr.class}', data='#{attr.data}')   

same for child tag

Patrick Lee

unread,
Apr 4, 2013, 5:56:16 PM4/4/13
to jad...@googlegroups.com
Thanks for the response but I will stick with my hack because I want something generic.  Maybe in the future attributes can be passed to tags as js objects instead of in the current name="value", form.  
Reply all
Reply to author
Forward
0 new messages