2016-01-05 23 views
0

我正在做Ember.js的第一步,我很困惑於社區提供的不同方法和解決方案。根據路線顯示/隱藏home按鈕

我有一個相當小的應用程序,它有一個應用程序模板和內容模板,可以通過鏈接指向。

我的應用程序模板有一個主頁按鈕,我想隱藏並顯示取決於我要導航的路線。我怎麼做?

我使用普通的JavaScript。沒有預處理器或類似的。

回答

2

您可以直接在控制器中的application controller hideHomeButtonRoutes使用全局currentRouteName財產

:[ '指數', '登錄'], isHomeButtonVisible:Ember.computed( 'currentRouteName',函數(){ 在template

{{#if isHomeButtonVisible}} <button>home</button> {{/if}} 
})

;返回this.get( 'hideHomeButtonRoutes')的indexOf(this.get( 'currentRouteName'))> -1。

這裏是工作example

+0

你可以指定路線和控制器爲您的解決方案嗎?我是否需要爲我的應用程序的每個路線設置setupController?或者它是一些應用程序路線?控制器是一種應用程序控制器嗎? –

+0

這只是做一次與你的主頁按鈕相同的水平,如果它是在應用程序模板把所有的應用程序路線,控制器 – Bek

+0

像這樣? App.ApplicationController = Ember.Controller.extend({ \t hideHomeButtonRoutes: ['index', 'intro'], \t isHomeButtonVisible: Ember.computed('currentRouteName', function() { \t \t return this.get('hideHomeButtonRoutes').indexOf(this.get('currentRouteName')) > 1; \t }) }); App.ApplicationRoute = Ember.Route.extend({ \t setupController: function (controller, model) { \t \t controller.set('currentRouteName', this.router.currentRouteName); \t }, }); 不知何故無法正常工作。我不明白。 –