【问题标题】:Custom If tag in jsrenderjsrender中的自定义If标记
【发布时间】:2016-04-11 17:03:09
【问题描述】:

我尝试创建一个自定义 if 标记 contains 来检查数组中的给定值。

jQuery.views.tags({
       contains:{
            baseTag: "if", //no I18n
            init: function(){
                var valueArr = this.tagCtx.args[0];
                var findValue = this.tagCtx.args[1];

                return _.contains(valueArr, findValue);
            }
        },
});

但它并没有像我预期的那样工作。需要帮助。

提前致谢。

【问题讨论】:

    标签: jsrender


    【解决方案1】:

    您正在覆盖 init 方法。它是你需要重写的渲染方法。

    http://www.jsviews.com/#tagshttp://www.jsviews.com/#samples/jsr/tags/extend-forhttp://www.jsviews.com/#samples/tag-controls/range

    jQuery.views.tags({
      contains:{
        baseTag: "if",
        render: function(valueArr, findValue) {
          if (valueArr && findValue) {
            // Call the base render method of the {{if}} tag,with the 'contains' result
            return this.base(_.contains(valueArr, findValue));
          }
          return this.base(); // {{else}} tag
        },
        // If using JsViews data-linking - need to override onUpdate too
        onUpdate: function(ev, eventArgs, tagCtxs) {
          return true;
        }
      }
    });
    

    用法:

    {{contains array1 item1}}
      ...
    {{else array2 item2}}
      ...
    {{/contains}}
    

    (或 {^{contains ...}} 如果使用 JsViews 数据链接...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多