javascript - Mustache template concatenation of expression with some string - Stack Overflow

How can we concatenate an expression value with some string and evaluate the resulting expression?If we

How can we concatenate an expression value with some string and evaluate the resulting expression?

If we have one object prefix and I wanted to concatenate its value with some string. e.g

{{prefix"Output"}}

I would like to first evaluate the prefix value and then concatenate it with the "Output" string.

So the resulting expression may be something like "UnitOutput" where Unit is the value of the prefix.

Lastly I want the concatenated expression from above to be evaluated as well in that case resulting value will be the value of "UnitOutput".

How can we concatenate an expression value with some string and evaluate the resulting expression?

If we have one object prefix and I wanted to concatenate its value with some string. e.g

{{prefix"Output"}}

I would like to first evaluate the prefix value and then concatenate it with the "Output" string.

So the resulting expression may be something like "UnitOutput" where Unit is the value of the prefix.

Lastly I want the concatenated expression from above to be evaluated as well in that case resulting value will be the value of "UnitOutput".

Share Improve this question asked Apr 24, 2015 at 8:03 RazaRaza 3101 gold badge4 silver badges11 bronze badges 1
  • I actually want to achieve something like this {{ {{prefix}}"Output" }} – Raza Commented Apr 24, 2015 at 11:12
Add a ment  | 

4 Answers 4

Reset to default 1

The keyword prefix did not work for me. I am assuming it is a reserved keyword. I will use pref in this example.

Html

<paper-input value="{{pref}}"></paper-input>
<span>Output: {{result}}</span>

Javascript

Polymer({
    pref: '',
    result: 'Output',
    prefChanged: function(oldValue, newValue) {
        this.result = newValue + 'Output';
        // evaluate result here
    }
}

Or you could use Polymers observer pattern

Html

<paper-input value="{{xyz.prefix}}"></paper-input>
<span>Output: {{result}}</span>

Javascript

Polymer({
    result: '',
    observe: {
        'xyz.prefix': 'validate'
    },
    validate: function(oldValue, newValue) {
        this.result = newValue + 'Output';
    },
    ready: function() {
        this.xyz = {
            prefix: 'test'
        }
    }
});

If you're observing Arrays please refer to observe-js

doing like

"{{variable}} Output"

should work

With RactiveJS it's simply:

{{ this[prefix + "Output"] }}

See http://jsfiddle/236bsky0/. this refers to current context or root, but can be another reference if you like.

You can also use a function : {{ getValue(variable) }} In your element, just declare your function : getValue: function(var) { return var + "Output";}

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信