javascript - Outputting a class with Rivets.js? - Stack Overflow

I thought this would be easier unless I'm missing something but I can't figure out how to out

I thought this would be easier unless I'm missing something but I can't figure out how to output a piece of my data as a class.

<div class="partner-type" rv-class="partner.partner-type"></div>

doesn't work. It should say the value in that property "technology" or "service". Is there anyway to do string interpolation or something? Data attrs?

I thought this would be easier unless I'm missing something but I can't figure out how to output a piece of my data as a class.

<div class="partner-type" rv-class="partner.partner-type"></div>

doesn't work. It should say the value in that property "technology" or "service". Is there anyway to do string interpolation or something? Data attrs?

Share Improve this question asked Apr 7, 2015 at 20:56 danheredanhere 6801 gold badge8 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

I ended up using something like

rv-class-ineededthisclass="partner.partner_type | isNotEqual 'premier'"

Btw, this library is great for rivetsjs: https://github./matthieuriolo/rivetsjs-stdlib

You can find in the official Github documentation page, a simple custom Binder which behaves exactly as you require: It assigns dynamically the className stored in your variable, to the bound element.

AddClass (rv-addclass)

Adds a new class to the element (using the attribute value) in addition to any existing ones. On subsequent changes, the previously added class is replaced with the new one.

Binder declaration:

rivets.binders.addclass = function(el, value) {
  if (el.addedClass) {
    $(el).removeClass(el.addedClass);
    delete el.addedClass;
  }

  if (value) {
    $(el).addClass(value);
    el.addedClass = value;
  }
};

Usage:

<i rv-addclass="partner.partner_type"></i>

I had the same issue. I have created a binder called "rv-set-class" to solve the problem.

rivets.binders['set-class'] = function(el, value){
     el.className += ' '+ value;   
}

You can now use it like this:

<div rv-set-class="partner.partner-type"></div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744767060a4592526.html

相关推荐

  • javascript - Outputting a class with Rivets.js? - Stack Overflow

    I thought this would be easier unless I'm missing something but I can't figure out how to out

    17小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信