简体   繁体   English

如何使用rivets.js解析值?

[英]How to parse value with rivets.js?

I am using Dashing framework based on Django. 我正在使用基于Django的Dashing框架。

HTML using the Rivets.js conventions to bind data to the script file. HTML使用Rivets.js约定将数据绑定到脚本文件。

<div rv-status-color="value">
    <h1>{ title }</h1>
    <h2>{ value }</h2>
    <p class="detail">{ detail }</p>
    <p class="more-info" rv-show="moreInfo">{ moreInfo }</p>
    <p class="updated-at" rv-show="updatedAt">{ updatedAt }</p>
</div>
<i rv-class="icon" rv-show="icon"></i>

Following script gets value from HTML and set neccessary color to .css according condition. 以下脚本从HTML获取值,并根据情况将必要的颜色设置为.css。

rivets.binders['status-color'] = function(el, value) {
    if (value == 0) {
        $(el).css('background-color', 'green');
    }
    else if (value < 0) {
        $(el).css('background-color', 'orange');
    }
    else {
        $(el).css('background-color', 'red');
    }
};

Could you tell me how to rewrite script to get {detail} value and comparing its with {value}? 您能告诉我如何重写脚本以获得{detail}值并将其与{value}进行比较吗?

Something like that: 像这样:

rivets.binders['status-color'] = function(el, value) {
    if (value == detail) {
        $(el).css('background-color', 'green');
    }
    else if (value < detail) {
        $(el).css('background-color', 'orange');
    }
    else {
        $(el).css('background-color', 'red');
    }
};

Thank you in advance. 先感谢您。

You can pass in object to your binder instead of static value. 您可以将对象而不是静态值传递给活页夹。

<div rv-status-color="yourObject">

And then use the object in script 然后在脚本中使用对象

rivets.binders['status-color'] = function(el, obj) {
    if (obj.value == obj.detail) {
        $(el).css('background-color', 'green');
    }
    else if (obj.value < obj.detail) {
        $(el).css('background-color', 'orange');
    }
    else {
        $(el).css('background-color', 'red');
    }
};

You just need to do the following: 您只需要执行以下操作:

rivets.binders['status-color'] = function(el, value, scope) { //scope being the current bound object
    var detail=scope.detail;
    if (value == detail) {
        $(el).css('background-color', 'green');
    }
    else if (value < detail) {
        $(el).css('background-color', 'orange');
    }
    else {
        $(el).css('background-color', 'red');
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM