Logic logic in the handle template.

Is it possible to carry out logical logic in the steering wheel symbols?

I am currently tricking this behavior with a controller function, so I end up with a controller

App.ApplicationController = Ember.Controller.extend({
    bool1: true,
    bool2: true,
    both: function(){ return this.bool1 && this.bool2; }.property('content.both'),
});

What allows me to use the steering wheel pattern

<script type="text/x-handlebars">
  {{#if both}}
     <p> both were true </p>
  {{/if}}
</script>

and it works fine, but causes some problems. Firstly, it obscures what happens (especially if good function names are not used). Secondly, it seems to violate the MVC split a bit.

Is it possible to do something along the lines

<script type="text/x-handlebars">
  {{#if bool1 && bool2}}  <!-- this will not actually work -->
     <p> both were true </p>
  {{/if}}
</script>

and does he work?

+5
source share
2 answers

You cannot do it directly, but it is not so difficult to do with a little argumentsparsing and a variational assistant. Something like that:

Handlebars.registerHelper('if_all', function() {
    var args = [].slice.apply(arguments);
    var opts = args.pop();

    var fn = opts.fn;
    for(var i = 0; i < args.length; ++i) {
        if(args[i])
            continue;
        fn = opts.inverse;
        break;
    }
    return fn(this);
});

:

{{#if_all a b c}}
    yes
{{else}}
    no
{{/if_all}}

{{#if_all}}, . , Handlebars, {{#if}}

`false`, `undefined`, `null`, `""` or `[]` (a "falsy" value)

, [] JavaScript.

: http://jsfiddle.net/ambiguous/vrb2h/

+7

, handlebars:

Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {

switch (operator) {
    case '==':
        return (v1 == v2) ? options.fn(this) : options.inverse(this);
    case '===':
        return (v1 === v2) ? options.fn(this) : options.inverse(this);
    case '<':
        return (v1 < v2) ? options.fn(this) : options.inverse(this);
    case '<=':
        return (v1 <= v2) ? options.fn(this) : options.inverse(this);
    case '>':
        return (v1 > v2) ? options.fn(this) : options.inverse(this);
    case '>=':
        return (v1 >= v2) ? options.fn(this) : options.inverse(this);
    case '&&':
        return (v1 && v2) ? options.fn(this) : options.inverse(this);
    case '||':
        return (v1 || v2) ? options.fn(this) : options.inverse(this);
    default:
        return options.inverse(this);
}

});

:

 {{#ifCond showDistance "&&" distance}}
      <span class="distance">
          {{distance}}
      </span>
 {{else}}
      {{#if showRegion}}
           <span class="region">
           </span>
      {{/if}}
 {{/ifCond}}
+9

All Articles