Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

ember.js - select dropdown with ember

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}}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...