Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select dropdown with ember

Tags:

ember.js

I'm trying to produce a select input and pass the selected object to the change event on the view. The ember contact example uses a <ul> but with a select the view needs to be outside the each otherwise the change even isn't fired.

Here is the view js:

App.SelectView = Ember.View.extend({

    change: function(e) {
        //event for select
        var content = this.get('content');
        console.log(content);   

        App.selectedWidgetController.set('content', [content]);
    },
    click: function(e) {
        //event for ul
        var content = this.get('content');
        console.log(content);   

        App.selectedWidgetController.set('content', [content]);
    }
});

The ul from the example works:

<ul>
    {{#each App.widgetController.content}}
        {{#view App.SelectView contentBinding="this"}}
            <li>{{content.name}}</li>
        {{/view}}
    {{/each}}
</ul>

But if I replace html directly, the change event isn't fired (which makes sense)

<select>
    {{#each App.widgetController.content}}
        {{#view App.SelectView contentBinding="this"}}
            <option>{{content.name}}</option>
        {{/view}}
    {{/each}}
</select>

So I guess the select has to be wrapped in the view.. in which case how do I pass the relevant object?... This code results in the entire array being passed:

{{#view App.select_view contentBinding="App.widgetController.content"}}
    <select>
    {{#each App.widgetController.content}}
        <option>{{name}}</option>
    {{/each}}
    </select>
{{/view}}
like image 555
Hayden Chambers Avatar asked Jan 18 '12 13:01

Hayden Chambers


3 Answers

Ember now has a built-in Select view.

Here's a usage example:

var App = Ember.Application.create();

App.Person = Ember.Object.extend({
    id: null,
    firstName: null,
    lastName: null,

    fullName: function() {
        return this.get('firstName') + " " + this.get('lastName');
    }.property('firstName', 'lastName').cacheable()
});

App.selectedPersonController = Ember.Object.create({
    person: null
});

App.peopleController = Ember.ArrayController.create({
    content: [
        App.Person.create({id: 1, firstName: 'Yehuda', lastName: 'Katz'}),
        App.Person.create({id: 2, firstName: 'Tom', lastName: 'Dale'}),
        App.Person.create({id: 3, firstName: 'Peter', lastName: 'Wagenet'}),
        App.Person.create({id: 4, firstName: 'Erik', lastName: 'Bryn'})
    ]
});

Your template would look like:

{{view Ember.Select
       contentBinding="App.peopleController"
       selectionBinding="App.selectedPersonController.person"
       optionLabelPath="content.fullName"
       optionValuePath="content.id"}}

Again, here's a jsFiddle example: http://jsfiddle.net/ebryn/zgLCr/

like image 105
ebryn Avatar answered Sep 29 '22 16:09

ebryn


check out the answers to a similar question: How to bind value form input select to attribute in controller

In the examples a CollectionView is used with an tagName=select. You may find this helpful in getting it work.

EDIT: Since I was looking to implement a select myself, here is the solution I came up with:

views/form.js.hjs:

{{#view contentBinding="App.typeController" valueBinding="type" tagName="select"}}
    {{#each content}}
        <option {{bindAttr value="title"}}>{{title}}</option>
    {{/each}}
{{/view}}
{{#view Ember.Button target="parentView" action="submitEntry"}}Save{{/view}}

The select is part of a form. I do check for the submit event and in there read the value:

app.js.coffee

# provides the select, add value: 'my_id' if you need differentiation
# between display name (title) and value
app.typeController = Ember.ArrayProxy.create
  content: [{title:'Energy'}, {title:'Gas'}, {title:'Water'}]

# simplified version, but should prove the point
app.form_view = Ember.View.create
  templateName: 'views_form'
  type: null
  submitEntry: () ->
    console.log this.$().find(":selected").val()

Hope this helps.

like image 34
dhenze Avatar answered Sep 29 '22 14:09

dhenze


This isn't an Answer, just a fix on the broken jsfiddle link.. Apparently jsfiddle has no love for ember :/ But JsBin does! http://jsbin.com/kuguf/1/edit

like image 45
micahblu Avatar answered Sep 29 '22 14:09

micahblu