简体   繁体   English

如何从Ember JS中的子路径访问父控制器?

[英]How to access the parent controller from the child routes in Ember JS?

I have a main route named list . 我有一个名为list的主路线。

//items/list/route.js
setupController(controller, items) { 
    let vegItems = Ember.A([]);
    let nonVegItems = Ember.A([]);
    items.forEach((item) => {
        if (item.get('category') === "veg") {
            vegItems.pushObject(item);
        }

        if (item.get('category') === "non_veg") {
            nonVegItems.pushObject(item);
        }
    })

    controller.set('vegItems', vegItems)
    controller.set('nonVegItems', nonVegItems);
}

Now inside the list route, I have the routes named veg and non-veg . 现在在列表路径中,我有名为vegnon-veg的路由。 ie list/veg and list/non-veg. 即list / veg和list / non-veg。 How can I access the controller variables of parent route, ie vegItems and nonVegItems from list/route.js to the child route to load the data in list/veg/template.hbs and list/non-veg/template.hbs? 如何从list / route.js中访问父路由的控制器变量,即vegItemsnonVegItems到子路由,以加载list / veg / template.hbs和list / non-veg / template.hbs中的数据?

this.controllerFor('list') - Returns the controller of the current route, or a parent (or any ancestor) route in a route hierarchy. this.controllerFor('list') - 返回当前路由的控制器,或路由层次结构中的父(或任何祖先)路由。 You can get all the properties through get method. 您可以通过get方法获取所有属性。

this.modelFor('list') - Returns the resolved model of a parent (or any ancestor) route in a route hierarchy. this.modelFor('list') - 返回路由层次结构中父(或任何祖先)路由的已解析模型。

list:Ember.inject.controller() - Creates a property that lazily looks up another controller in the container. list:Ember.inject.controller() - 创建一个懒惰地查找容器中另一个控制器的属性。 You can inject it only in the controller. 您只能在控制器中注入它。

Reference: 参考:

https://emberjs.com/api/ember/2.14/classes/Ember.Route/methods/controllerFor?anchor=controllerFor https://emberjs.com/api/ember/2.14/classes/Ember.Route/methods/controllerFor?anchor=controllerFor

https://emberjs.com/api/ember/2.14/classes/Ember.Route/methods/controllerFor?anchor=modelFor https://emberjs.com/api/ember/2.14/classes/Ember.Route/methods/controllerFor?anchor=modelFor

https://www.emberjs.com/api/ember/2.14/namespaces/Ember.inject/methods/controller?anchor=controller https://www.emberjs.com/api/ember/2.14/namespaces/Ember.inject/methods/controller?anchor=controller

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

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