2012-03-04 59 views
3

我想根據視圖屬性的值在{{#with}}範圍內切換元素的可見性。 我可以在{{#with}}中的{{action}}中查看屬性,但我無法訪問{{with}中的屬性。我可以在{{#with}}範圍內訪問視圖屬性嗎?

下面是一個示例程序。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.6.1.min.js"%3E%3C/script%3E'))</script> 
<script src="http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.5.js"></script> 

<script type="text/javascript"> 
    var App = Em.Application.create(); 
    App.Test = Em.View.extend({ 
    content: { 
     description: 'names', 
     names: [{name: 'bob'}, {name: 'john'}] 
    }, 
    viewDetail: false, 
    toggleDetail: function() { 
     this.set('viewDetail', true); 
    } 
    })   
</script> 

<script type="text/x-handlebars"> 
    {{#view App.Test}} 
    {{#with content}} 
     {{description}}<br/> 
     {{#each names}} 
     {{name}} 
     {{/each}} 
     <br/> 
     <button {{action toggleDetail}}>toggle Detail</button> <!-- can access toggleDetail --> 
     {{#if viewDetail}} <!-- but can not access viewDetail... --> 
     …detail Description... 
     {{/if}} 
    {{/with}} 
    {{/view}} 
</script> 

我可以在{{#with}}範圍內查看屬性嗎?

回答

6

#with修改Handlebars上下文。您可以使用../進行關卡備份。

試試這個:

{{#if ../viewDetail}} 
+0

可以訪問!謝謝! – arumons 2012-03-05 12:46:40

+0

你碰巧知道如何在{{#if}}塊中恢復關卡? – Fadi 2012-03-30 18:51:12

+0

@Fadi,你只需在content.'前添加屬性或者添加一個新的'with'塊。 – 2012-03-30 18:53:55

相關問題