1

我想使用 Ember.Select 值过滤我的 JSON,而不是对其进行硬编码。这是我的 App.js

App = Ember.Application.create({});

App.IndexRoute = Ember.Route.extend({
    renderTemplate : function(controller) {
        this.render('MyApp', {
            controller : controller
        });
    },
    model : function() {
        return App.MyTemplateModel.find();
    }
});

App.IndexController = Ember.ArrayController.extend({

    filteredContent : Ember.computed.oneWay("content"),
    last : function() {
        var filtered = this.get('content').filterProperty('last_name', "Solow");
        this.set("filteredContent", filtered);

    }
});

App.MyTemplateModel = Ember.Model.extend({
    id : Ember.attr(),
    last_name : Ember.attr(),
    first_name : Ember.attr(),
    suffix : Ember.attr(),
    expiration : Ember.attr()
});

App.controller = Ember.Object.create({
    selectedProgrammer : null,
    content : [Ember.Object.create({
        last_name : "Solow",
        id : 1
    }), Ember.Object.create({
        last_name : "Tom",
        id : 2
    })]
});

App.MyTemplateModel.url = "http://ankur1.local/index.php/api/example/users/";
App.MyTemplateModel.adapter = Ember.RESTAdapter.create();
var existing = App.MyTemplateModel.find();
App.MyTemplateModel.camelizeKeys = true;

在行中,

 var filtered = this.get('content').filterProperty('last_name', "Solow");

我正在对“Solow”进行编码,但我想使用 Ember.Select 值。

我试过这样做,

 var selectedProgrammer = App.controller.get("selectedProgrammer");
 var filtered = this.get('content').filterProperty('lastName', App.controller.get('selectedProgrammer'));
 this.set("filteredContent", filtered);

但它什么也没做,此外,这就是我在 HTML 上所做的,

<script type="text/x-handlebars" data-template-name="MyApp">
{{#each item in filteredContent }}
            <tr><td>
            {{id}} <p> {{item.first_name}} {{item.expiration}}</p>
            </td></tr>
            {{/each}}

            <button {{action last}}>filter</button>

            {{view Ember.Select
            contentBinding="App.controller.content"
            optionValuePath="content.id"
            optionLabelPath="content.last_name"
            selectionBinding="App.controller.selectedProgrammer"}}
</script>

此外,这里有一个 JSON 样本,http ://pastebin.com/7dyNQi4K

我可能在哪里出错或我可能需要做出哪些改变?

4

1 回答 1

3

Your Ember.Selectis bound to App.controller.selectedProgrammer, when the select changes, it will receive an object. 因此,在您的控制器中,您可以last_name使用App.controller.get('selectedProgrammer.last_name').

App.IndexController = Ember.ArrayController.extend({
    filteredContent : Ember.computed.oneWay("content"),
    last : function() {
        var lastName = App.controller.get('selectedProgrammer.last_name');
        var filtered = this.get('content').filterProperty('last_name', lastName);
        this.set("filteredContent", filtered);
    }
});
于 2013-08-07T23:14:45.927 回答