我注意到 Ember 中的一种行为对我来说没有任何意义。我不确定这是否是一个错误或功能。在后一种情况下,我真的很感兴趣为什么这是一种理想的行为。那么我们开始吧:
- 确保您可以看到浏览器控制台输出。
- 打开example project on JS Bin 。请注意来自
IndexController
和FoobarController
的两条初始化消息。 - 点击“添加一个”按钮。执行此操作,以便 FoobarController 上有某种状态。
- 点击“Go to hello”链接即可转换到 hello 路线
- 通过链接返回索引
- 计数变量仍然具有值。一切都好!
现在有一个tiny change in the next JS Bin 。我将模型传递给渲染助手。 再次按照上述步骤操作。在第 5 步之后,您会看到计数现在为 0,并且“init FoobarController”再次出现。
当传递模型时,属于渲染助手的 Controller 会以某种方式重置。我找不到任何关于为什么会发生这种情况的信息,也无法想到为什么这是有意义的。
最佳答案
<强> From the Docs
If a model property path is specified, then a new instance of the controller will be created and {{render}} can be used multiple times with the same name.
传递第二个参数会重新实例化 FoobarController
,这基本上会将计数重置为 0,而不传递模型参数会创建 FoobarController
的单例实例。
关于javascript - 如果传递模型,属于渲染助手的 Controller 将被重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745630/