在 Ember 指南中我们可以找到以下示例:
{{view Ember.Select viewName="select" contentBinding="App.peopleController" optionLabelPath="model.fullName" optionValuePath="model.id" prompt="Pick a person:" selectionBinding="App.selectedPersonController.person"}}
传入 View 的变量之一绑定(bind)到 PeopleController,但其余部分(例如 optionValuePath
)也连接到应用程序的其他部分。
我有点困惑,因为有时我不知道应该使用哪一个。但所有这些都可以在 View 内访问。
optionLabelPath
(无“-Binding”)是单向绑定(bind)还是只是一次性传递值?
有什么区别?
最佳答案
当您使用 View 助手从 Handlebars 创建 View 实例时,所有(好的,most)名称=值对都将传递给 View 的创建方法。所以:
{{view Ember.Select viewName="select" contentBinding="App.peopleController" optionLabelPath="model.fullName" optionValuePath="model.id" prompt="Pick a person:" selectionBinding="App.selectedPersonController.person"}}
变成
view = Ember.Select.create({
contentBinding="App.peopleController"
optionLabelPath="model.fullName"
optionValuePath="model.id"
prompt="Pick a person:"
selectionBinding="App.selectedPersonController.person"
})
因此,由于 Ember 的“自动创建带有/^*Binding/-NAMED PROPERTIES 的绑定(bind)”功能 - 请参阅 Ember Binding API docs 。
Is optionLabelPath (no '-Binding') the one-way binding or it just passes in the value once and for all?
其他 3 个(无“-Binding”)属性没有什么特别之处。每个都被设置为一个简单的字符串。 Ember.Select
将循环遍历内容数组 (App.peopleController) 并使用 optionLabelPath
从数组中的每个项目查找标签。 optionValuePath
用于查找值。
What's the difference?
因此,任何以 Binding
结尾的内容都需要指向其他对象的路径。其他所有内容都只是作为字符串传递。 Ember.Select 碰巧以特殊方式使用了两个 Path
属性,但这只是一种命名约定。
关于ember.js - View 中带有和不带有绑定(bind)后缀的变量 - 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16752203/