javascript - 如果传递模型,属于渲染助手的 Controller 将被重置

标签 javascript ember.js

我注意到 Ember 中的一种行为对我来说没有任何意义。我不确定这是否是一个错误或功能。在后一种情况下,我真的很感兴趣为什么这是一种理想的行为。那么我们开始吧:

  1. 确保您可以看到浏览器控制台输出。
  2. 打开example project on JS Bin 。请注意来自 IndexControllerFoobarController 的两条初始化消息。
  3. 点击“添加一个”按钮。执行此操作,以便 FoobarController 上有某种状态。
  4. 点击“Go to hello”链接即可转换到 hello 路线
  5. 通过链接返回索引
  6. 计数变量仍然具有值。一切都好!

现在有一个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/

相关文章:

javascript - 如何在没有服务器的情况下使用 ember.js

events - Ember : Specify a target action view for an action helper, 除了包含 View 或父 View 之外

比较两个值时,JavaScript 似乎评估出无效结果

javascript - jQuery 查找一个元素

javascript - 正则表达式检查字符串中是否有任何单词不超过限制

javascript - Ember js 2.0 将计算属性从 Controller 传递到组件

JavaScript onclick 函数只工作一次(非常简单的代码)

javascript - JS-网格 : How to set default value

ember.js - 将文本字段绑定(bind)到 ember 中的模型的最佳方法

javascript - 使用 Ember.js 自动完成