我在Ember上做事情时遇到了麻烦,而且我确定这是因为我还没有完全掌握“Ember方式”的做事方式,而我正在尝试去做一些超出标准教程范围的东西。在emberjs上设计一个自定义的组件/视图
我正在开发某种textfield-with-suggestions组件,它将出现在我的web应用程序的每个页面中。我不会在这里问到如何做到这一点的所有细节,而只是一些我从一开始就遇到麻烦的具体事情。以下是迄今为止我所拥有的相关代码片段。
// handlebars template: searchbar.hbs
{{view App.SearchField viewName="searchField"}}
<div class="results" {{bindAttr class="searchField.hasFocus"}}>
This is where the results for whatever the user has typed are shown.
</div>
// coffeescript source of the views: searchbar.coffee
App.SearchBar: Ember.View.extend
templateName: 'searchbar'
App.SearchField: Ember.TextField.extend
placeholder: 'Search'
hasFocus: false
eventManager: Ember.Object.create
focusIn: (event, view) ->
@set('hasFocus', true)
focusOut: (event, view) ->
@set('hasFocus', false)
// somewhere in the layout of the pages in my app
<div id="header">
{{App.SearchBar}}
</div>
这可能还需要一个控制器,但我还没有开发它,因为我不知道它这个设置中还配合。
首先,我希望在搜索字段获得焦点时立即显示建议弹出窗口。这是我尝试在搜索字段上实现hasFocus属性的原因。但我如何实现让我的div.results面板对输入字段的焦点状态做出反应?
一般来说,这是我的问题的核心在这里,我如何连接所有的东西来开发这个组件?如果答案是通过将它附加到控制器上,那么我如何为这个组件设置一个控制器,以及如何指定它是它的控制器,以便它充当一切的上下文?
要添加控制器,在您的视图类中,您可以说'controllerBinding:'App.YourContorller',并且该视图将与'controller'对话。此外,你可能想看看['classNameBindings'](http://emberjs.com/api/classes/Ember.View.html#property_classNameBindings)。这里有一个使用它的示例:http://jsfiddle.net/schawaska/7Uk7J/ – MilkyWayJoe