2

I've got an Ember.js app that uses the Ember.Router structure.

My app structure looks something like

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'

So controllers and view are extended and not created on application creation. There are then routes that connect outlets

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

I need to bind a <select> tag to a set of values. Ember.Select looks like a good way to do this so I add a controller and view extension for the 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 doesn't work. I get an error about the this.get('controller') when I try to include inside a view with {{#view App.MySelectView}}

How do I do this right?

sly7_7
  • 11,961
  • 3
  • 40
  • 54
wmarbut
  • 4,595
  • 7
  • 42
  • 72

1 Answers1

6

Here is an example of how I would implement this: the jsfiddle

Handlebars template:

<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();

sly7_7
  • 11,961
  • 3
  • 40
  • 54
  • I've updated by adding a textfield, with value bound on the select value. It's almost the same code, but with a little more complex context object maintained by the form. By extension, you can do what you want. – sly7_7 Jul 26 '12 at 15:03