简体   繁体   English

Ember JS-从路由和附加动态渲染模板

[英]Ember JS - Dynamically render template from route and append

I have a web application (using ember 2.0) with 2 parts. 我有一个由2部分组成的Web应用程序(使用ember 2.0)。 Left menu and right content, both independent from each other. 左菜单和右内容彼此独立。 Navigating to route name "PageRoute", application load the configuration JSON from server, process it and the checks, where should be content rendered: 导航到路由名称“ PageRoute”,应用程序从服务器加载配置JSON,对其进行处理和检查,应在何处呈现内容:

{
...
position: "left",
...
}

or 要么

{
...
position: "right",
...
}

Then I use "this.transitionTo" and redirect it to RightRoute or LeftRoute. 然后,我使用“ this.transitionTo”并将其重定向到RightRoute或LeftRoute。 RightRoute is ok, LeftRoute is the problem. RightRoute没问题,LeftRoute是问题。

At the beginning - Left menu is empty. 一开始-左侧菜单为空。 When LeftRoute is loaded, it should append (not replace) some menu (rendered template or component). 加载LeftRoute时,应附加(而不是替换)某些菜单(呈现的模板或组件)。 When the LeftRoute is loaded again, new menu should be rendered from model and append it to existing left menus so it would be possible to swap between rendered menus and the last menu will be visible. 当再次加载LeftRoute时,应从模型中渲染新菜单并将其附加到现有的左菜单中,以便可以在渲染菜单之间进行交换,并且最后一个菜单将可见。 At the same time, when the current menu is swapped, it should be removed from the "left menu list" and the previous menu will be visible. 同时,交换当前菜单时,应将其从“左侧菜单列表”中删除,并且上一个菜单将可见。

I've found some some solutions appending View, but the view is deprecated in Ember 2.0, I've tried to do it like here http://emberjs.jsbin.com/defapo/3/edit?html,js,output but Router can't access actions in components (with pushObject) and I've tried countless other methods like using data stores or dynamically modifying the model etc. 我发现了一些附加视图的解决方案,但是该视图在Ember 2.0中已被弃用,我尝试在此处进行操作,例如http://emberjs.jsbin.com/defapo/3/edit?html,js,output但路由器无法访问组件中的操作(使用pushObject),我尝试了无数其他方法,例如使用数据存储或动态修改模型等。

I would make it this way: 我会这样:

/controllers/left.js /controllers/left.js

import Ember from 'ember';
export default Ember.Controller.extend({

    menus: [],

    addMenu (menuData) {
        this.get('menus').addObject(menuData);
    },

    removeMenu () {
        this.get('menus').popObject();
    }

});

/routes/left.js /routes/left.js

import Ember from 'ember';
export default Ember.Route.extend({

    setupController (controller, model) {
        controller.addMenu(model);
    }

});

/templates/left.hbs /templates/left.hbs

{{#each menus as |menu| }}
    <!-- render menu -->
    {{log menu}}
{{/each}}

In this case when you transitionTo('left', menuData) it will append the menu to the list. 在这种情况下,当您transitionTo('left', menuData) ,它将菜单添加到列表中。 When you need to remove last menu, you can do this.controllerFor('left').removeMenu() in any other route (eg in your PageRoute). 当您需要删除上一个菜单时,可以在任何其他路由中(例如在PageRoute中)执行this.controllerFor('left').removeMenu() )。

Hope this helps. 希望这可以帮助。

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

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