简体   繁体   English

Ember.js-仅在单击时在application.hbs上显示子导航,而无需呈现其他模板

[英]Ember.js - having a subnav display on application.hbs on click only without rendering a different template

I'm having issues trying to get a subnav to display on click in Ember. 我在尝试让Subnav在Ember中显示时遇到问题。 In my application template I have a nav bar, and at the root I have the index template which is a full browser width template sitting behind the navbar on application template. 在我的应用程序模板中,我有一个导航栏,在根处有索引模板,它是一个完整的浏览器宽度模板,位于应用程序模板上导航栏的后面。 Looks like this: 看起来像这样:

在此处输入图片说明

What I want to happen is when 'ABOUT' is clicked, a subnav displays on the white horizontal bar directly below the main nav. 我要发生的是单击“关于”时,一个子导航显示在主导航正下方的白色水平栏中。 The white horizontal bar is also part of the application template. 白色的水平条也是应用程序模板的一部分。 That's the only thing on the page that I want to change though when 'ABOUT' is clicked. 那是页面上唯一要更改的内容,尽管单击了“ ABOUT”。 Then when when you click an item on the subnav, say 'STAFF' it renders the about.staff template. 然后,当您单击子导航上的项目时,说出“ STAFF”,它将呈现about.staff模板。

My problem is getting this to happen on the application template. 我的问题是在应用程序模板上发生这种情况。 Because if the user is currently on the 'PROGRAMS' template, and then they click about, I want the user to stay on the programs template but the subnav to still drop down below the main nav. 因为如果用户当前在“程序”模板上,然后他们单击一下,则我希望用户继续在程序模板上,但是子导航仍下拉到主导航下方。

I've tried nested routes: 我已经尝试过嵌套路线:

Ew.Router.map ->
  @.resource "about", ->
    @.route "philosophy"
    @.route "leadership"
    @.route "staff"
    @.route "affiliations"
  @.route "conditions"
  @.route "programs"
  @.route "testimonials"

Then I tried rendering a named outlet in application hbs with the following ApplicationRoute 然后我尝试使用以下ApplicationRoute在应用程序hbs中渲染命名的插座

Ew.ApplicationRoute = Ember.Route.extend(
  renderTemplate: ->
    @.render
    @.render 'about',
        outlet: 'about',
        into: 'application'
)

But I'm just getting an error: 但是我刚得到一个错误:

Error while loading route: TypeError {} ember.js?body=1:382
Uncaught TypeError: Cannot call method 'connectOutlet' of undefined 

I would like to do this without having to hack a bunch of jquery into it. 我想做到这一点而不必将大量的jquery拖入其中。 I hope this makes sense, I really appreciate any help. 我希望这是有道理的,非常感谢您的帮助。

The way I've set up my sub nav, is just using a sub-nav.hbs template, and a SubNavController to manage the active state. 我设置sub nav的方式只是使用sub-nav.hbs模板和SubNavController来管理活动状态。 I render it from my main template like this: {{render 'sub-nav'}} You could write code in your SubNavController to determine which links to show. 我可以从我的主模板中进行渲染,如下所示:{{render'sub-nav'}}您可以在SubNavController中编写代码以确定要显示的链接。 Hope this helps a little. 希望这有所帮助。

Here's my SubNavController. 这是我的SubNavController。 This is for something like a "wizard" flow, so I don't want all the links to be enabled. 这是针对“向导”流的,所以我不希望启用所有链接。 (I'm also using an Ember StateManager object to manage the state of my app.) (我也在使用Ember StateManager对象来管理我的应用程序的状态。)


MyApp.SubNavController = Ember.Controller.extend({
  getAllStates: function() {
    return [
      { name: "start", label: "Start", active: true, enabled: true, href: "#/myapp/start"},
      { name: "drivers", label: "Driver", href: "#/myapp/driver"},
      { name: "vehicles", label: "Vehicle", href: "#/myapp/vehicle"},
      { name: "details", label: "Details", href: "#/myapp/details"}
    ];
  },

  init: function() {
    this.set('states', this.getAllStates());
    this.setActive();
  },

  setActive: function() {
    // TODO: Clean this up.  it's a little hacky.
    Ember.Logger.debug("current state: " + MyApp.stateManager.get('currentState.name'));
    var i = 0,
        newStates = this.getAllStates(),
        statesLength = newStates.length,
        activeIndex = 0;
    for (i=0; i< statesLength; i++) {
      newStates[i].active = false;
      if (newStates[i].name===MyApp.stateManager.get('currentState.name')) {
        newStates[i].active = true;
        activeIndex = i;
      }
    }
    for(i=activeIndex; i<statesLength; i++) {
      delete newStates[i].href;
    }
    this.set('states', newStates);
  }.observes('MyApp.stateManager.currentState')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM