2

我有一个使用Ember.Router 结构的 Ember.js 应用程序。

我的应用程序结构看起来像

window.App = Ember.Application.create {
    #Truncated idea of app, not including application controller or any of that
    MainController = Ember.Controller.extend()
    MainView = Ember.View.extend
        templateName: 'main-template'

因此控制器和视图是扩展的,而不是应用程序创建时创建的。然后有连接出口的路线

Router: Ember.Router.extend
    root: Ember.Route.extend
        main: Ember.Route.extned
            route: '/'
            connectOutlets: (router, event) ->
                router.get('applicationController').connectOutlet('main')

我需要将<select>标签绑定到一组值。Ember.Select看起来是一个很好的方法,所以我为选择添加了一个控制器和视图扩展

MySelectController: Ember.ArrayController.extend
    contents: [{id:1,title:'test'},{id:2,title:'test2'}]
MySelectView: Ember.Select.extend
    contentBinding: this.get('controller')
    contentValuePath: 'id'
    contentLabelPath: 'title'

这行不通。this.get('controller')当我尝试包含在视图中时,我收到一个错误{{#view App.MySelectView}}

我该怎么做?

4

1 回答 1

6

这是我将如何实现的示例:jsfiddle

车把模板:

<script type="text/x-handlebars" data-template-name="application">
  <header>
    <h1>Ember Router Sample</h1>
  </header>
  {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="myForm">
  {{view view.mySelectView 
    selectionBinding="controller.selected" 
    contentBinding="view.controller.content.persons"
  }}
  {{view Ember.TextField valueBinding="controller.selected.name"}}
</script>

javascript:

App = Ember.Application.create();

App.ApplicationController = Em.Controller.extend();
App.ApplicationView = Em.View.extend({
  templateName: 'application'
});

App.MySelectController = Em.ArrayController.extend();

App.MyFormController = Em.Controller.extend({
  selected: null,
});

App.MyFormView = Ember.View.extend({
  templateName: 'myForm',
  mySelectView: Em.Select.extend({
    optionLabelPath: 'content.name',
    optionValuePath: 'content.id'
  })
});

App.Router = Em.Router.extend({

  root: Em.Route.extend({

    index: Em.Route.extend({
      route: '/',
      connectOutlets: function(router, context) {
        router.get('applicationController').connectOutlet({
          name: 'myForm',
          context: Ember.Object.create({
              persons: [{id:0, name: "Wayne"}, {id: 1, name: "Gart"}]
          })
        });
      }
    })
  })
});
App.initialize();

​</p>

于 2012-07-25T23:05:20.367 回答