助手实例化一个控制器类

助手实例化一个控制器类

本文介绍了使用{{view}}助手实例化一个控制器类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然 connectOutlet(basename)自动创建一个 BasenameController 的实例,但我想知道是否有办法使用

While connectOutlet("basename") automatically creates an instance of BasenameController, I was wondering if there's a way to do the same using the {{view}}-helper.

我已经尝试了几个例子中看到的东西,但非他们似乎工作:

I have tried several things I've seen in examples, but non of them seem to work:

我也尝试使用 controller 而不是 controllerBinding ,不幸的是没有成功,我也无法找出两者之间的差异在哪里。

I have also tried to do the same using controller instead of controllerBinding, unfortunately without success, and I was also unable to find out where exactly the difference is between the two of them.

有人知道如何实现我的目标?

Does anybody know how to achieve my goal?

推荐答案

您可能想使用插座。 connectOutlet / outlet功能用于重新设置其他控制器/视图对。

You probably want to use an outlet. The connectOutlet/outlet functions are meant for rending other controller/view pairs.

说我们有一个人的视图,但在这个视图中,我们想要另一个控制器/视图对。为此,我们需要使用一个指定的插座,我们的模板将如下所示。

Lets say we have a person view, but inside that view we want to have another controller/view pair. For this, we need to use a named outlet, our template would look like this.

Person View!
{{name}} = the person's name!
{{controller}} = PersonController!
{{outlet other}} = our outlet

然后在路由器内部将另一个控制器/视图附加到该插座,您可以在personController上简单使用connectOutlet。

Then inside the router when you want to attach another controller/view to that outlet you can simple use connectOutlet on the personController.

router.get('personController').connectOutlet('other', 'other');

将OtherController和OtherView连接在一起,并将其显示在模板中。第一个参数是插座名称,第二个是控制器/视图。

That will wire OtherController and OtherView together and display them in your template. The first param is the outlet name, the 2nd is the controller/view.

这允许您轻松地将不同的控制器和视图交换到该插座。例如,使用不同的connectOutlet api,我们可以

This allows you to easily swap different controllers and views onto that outlet. For example, using a different connectOutlet api, we could

router.get('personController').connectOutlet({
 outletName: 'other',
 controller: router.get('carsController'),
 viewClass: App.CarsView
});

...

Btw,to回答你原来的问题。您可以通过以下方式访问其他控制器: {{view controllerBinding =controller.target.otherController}} 。每个控制器都有一个目标属性指向路由器。但是,我不建议使用此代码。它很脆弱,很难测试,很难调试,并将来会再次咬你。

Btw, to answer you original question. You can get access to other controllers from your view by doing this: {{view controllerBinding="controller.target.otherController"}}. Every controller will have a target property that points back to the router. However, I do not recommend using this code. It's brittle, hard to test, hard to debug, and will come back and bite you in the future.

这篇关于使用{{view}}助手实例化一个控制器类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:25