2015-04-23 53 views
0

我有一個組件根據用戶權限隱藏設計的一些部分。Ember:如何添加「其他」來包裝組件?

只隱藏不是問題。有時我需要在用戶無法訪問時顯示某些內容。

路徑模板:

{{#restricted-access required-action="AddRoles"}} 
    <button class="btn btn-success" {{action 'add'}}> 
     Add New Role 
    </button> 
{{else}} 
    You can not add Roles 
{{/restricted-access}} 

組件校驗碼:

actionAllowed: function() { 
    var permission = this.get('current.user.role.roleActionList'); 
    return permissions.filterProperty('code', this.get('required-action')).length > 0; 
}.property('current.user.role.roleActionList.length') 

組件模板:

{{#if actionAllowed}} 
    {{yield}} 
{{/if}} 

我正在尋找類似

{{#if actionAllowed}} 
    {{yield}} 
{{else}} 
    {{else yield}} 
{{/if}} 

我可以在哪裏添加路由模板中定義的文本。

我想我可以做這樣的事情:

{{#restricted-access required-action="AddRoles"}} 
    {{#access-granted}} 
     <button class="btn btn-success" {{action 'add'}}> 
      Add New Role 
     </button> 
    {{#access-granted}} 
    {{#access-declined}} 
     You can not add Roles 
    {{/access-declined}} 
{{/restricted-access}} 

其實我不喜歡這是我不得不創建附加組件。

有什麼辦法可以在一個組件中實現所需的功能嗎?

回答

1

由於您的其他塊僅是一個字符串,我只想傳遞參數與其他文字:

{{#restricted-access required-action="AddRoles" noAccessText="You can not add Roles"}} 
    <button class="btn btn-success" {{action 'add'}}> 
     Add New Role 
    </button> 
{{/restricted-access}} 

模板組件:

{{#if actionAllowed}} 
    {{yield}} 
{{else}} 
    {{noAccessText}} 
{{/if}} 
+0

這只是一個例子。 'else'塊中可以有不同的內容 – dex07

+0

您當然也可以使用屬性而不是硬編碼的字符串!因此,在您的控制器/路由中創建了一個道具,並使用該屬性而不是硬編碼的字符串。 –

0

組件應該是不可知的你的控制器/模型,它更像是顯示/動作功能。

我建議你使用控制器的方法和if檢查的權利:

{{#if restriction.canAddroles}} 
..actions... 
{{else}} 
    You can not add Roles 
{{/if}} 

restriction可以注射使用初始化所需的控制器。

#... restriction class 
export Ember.Object.create 
    canAddroles: -> 
    @actionAllowed('addRoles') 

    actionAllowed: -> 
    # code from your post 

我知道這是更冗長,因爲if限制,但現在你的邏輯存儲在適當的地方,它是更容易測試。