2013-02-12 19 views
5

Bir handlebar içinde koşul mantığı mantıksal mantık yürütmek mümkün mü? ,Boolean mantığı bir handlebars şablonu içinde

Şu anda bir kontrolör fonksiyonu ile bu davranışı taklit, bu yüzden beni

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

bir gidonu şablonunu kullanmasını sağlar denetleyici

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

ile bitirmek ve bu iyi çalışıyor ama bazı problemleri gündeme getiriyor. İlk olarak, neler olduğunu gizler (özellikle iyi işlev adları kullanılmazsa). İkincisi, MVC ayırma biraz ihlal gibi görünüyor.

o

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

çizgisinde bir şey yapmak ve bu işe sahip olabilir mi?

+0

ilgili bkz: http://stackoverflow.com/questions/14149415/double-condition-with-if – CraigTeegarden

cevap

7

Doğrudan yapamazsınız, ancak biraz arguments ayrıştırma ve değişken bir yardımcı ile yapmak o kadar da zor değil. Böyle bir şey:

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); 
}); 

Sonra bir şablona şunları söyleyebilirsiniz: İhtiyacınız kadar

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

Sen {{#if_all}} olarak birçok argüman kullanabilirsiniz. Sen {{#if}} davranır

Falsey ve JavaScript truthy olan [] oysa truthy olarak her şey olarak
`false`, `undefined`, `null`, `""` or `[]` (a "falsy" value) 

beri Gidonu maç için truthiness testini ayarlamak isteyebilirsiniz.

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

9

bu yardımcı Gidonu deneyebilirsiniz çekebilir:

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); 
} 

});

ve şu şekilde çağırmak:

{{#ifCond showDistance "&&" distance}} 
     <span class="distance"> 
      {{distance}} 
     </span> 
{{else}} 
     {{#if showRegion}} 
      <span class="region"> 
      </span> 
     {{/if}} 
{{/ifCond}} 
İlgili konular