简体   繁体   English

Ember.js:如何在选择助手上使用观察者来过滤内容?

[英]Ember.js: How to use an observer on select helper to filter content?

I'm trying to achieve the following with Ember-CLI: 我正在尝试通过Ember-CLI实现以下目标:

After an initial list of items is loaded, the user can select a city from the dropdown to see only those items that are interesting to him/her. 加载项目的初始列表后,用户可以从下拉列表中选择城市,以仅查看他/她感兴趣的项目。 In my case that's districts in cities. 就我而言,这是城市中的地区。 You can select from a dropdown list a city to see only districts in that city. 您可以从下拉列表中选择城市,以仅查看该城市中的地区。

Ideally, all should happen without calling an separately (on click). 理想情况下,所有操作都应在不单独调用(单击)的情况下进行。

So far, I've got this, but the console.log for 'filteredContent' returns an array of 0 elements. 到目前为止,我已经知道了,但是'filteredContent'的console.log返回了一个包含0个元素的数组。 Any hints where I'm doing something wrong? 有什么提示我在做错什么吗?

district/index.hbs: 区/ index.hbs:

<p>{{view "select" content=cities optionValuePath="content.id" optionLabelPath="content.name" selection=selectedCity}}</p>
{{#each item in filteredContent}}
    <p>{{item.name}} in {{item.city.name}}</p>
{{/each}}

route: 路线:

var DistrictListRoute = Ember.Route.extend({
    model: function () {
        return this.store.find('district');
    },

    setupController: function(controller, model) {
        this._super(controller, model);
        this.store.find('city').then(function(cities) {
            controller.set('cities', cities);
        });
    }
});

export default DistrictListRoute;

controller: 控制器:

export default Ember.Controller.extend({
    filteredContent: [],
    selectedCity: null,

    selectedCityChanged: function () {
        var selectedCity = this.get('selectedCity');
        console.log(selectedCity);
        var filteredContent = this.get('model').filterBy('city', selectedCity);
        console.log(filteredContent);
    }.observes('selectedCity')
});

model: 模型:

export default DS.Model.extend({
  city: DS.belongsTo('city', {async: true}),
  name: DS.attr('string'),
  advert: DS.hasMany('item')
});

Finally figured it out: 终于弄清楚了:

Controller: 控制器:

export default Ember.ArrayController.extend({
    selectedCity: null,
    filteredContent: [],

    selectedCityChanged: function () {
        var selectedCity = this.get('selectedCity');
        var filteredContent = this.get('model').filterBy('city.id', selectedCity.id);
        this.set('filteredContent', filteredContent);
    }.observes('selectedCity')

And then, handlebars template needed some tweaking: 然后,车把模板需要进行一些调整:

<p>{{view "select" content=cities optionValuePath="content.id" optionLabelPath="content.name" selection=selectedCity}}</p>
    {{#if filteredContent}}
        <h2>District in {{selectedCity.name}}</h2>
        {{#each district in filteredContent}}
            <p>{{district.name}}  in {{district.city.name}}</p>
        {{/each}}
        {{else}}
        <h2>Districts</h2>
        {{#each district in content}}
            <p>{{district.name}}  in {{district.city.name}}</p>
        {{/each}}
    {{/if}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM