将ember-select-2与ember Data一起使用



我使用的是https://github.com/iStefo/ember-select-2在我的Ember.js项目中。项目列表相当简单,使用路线中的Ember数据加载:

    setupController: function(controller, model) {
    this._super(controller, model);
    var otherModelsPromises = {
        accomodationTypes: this.store.find('accomodation-type'),
    };
    Ember.RSVP.hash(otherModelsPromises).then(function(results) {
        controller.set('accomodationTypes', results.accomodationTypes.get('content'));

    });

在我的模板中,我有:

{{select-2 placeholder="Filter By" content=accomodationTypes cssClass="search-filter-dropdown"}}

这是从URL返回的JSON(http://localhost:4200/api/accomodationTypes)

{"accomodationTypes":[{"id":1,"text":"Bed & Breakfast","description":"","svgid":""},
{"id":2,"text":"Self Catering","description":"","svgid":""},
{"id":3,"text":"Hotel","description":"","svgid":""},
{"id":4,"text":"Boutique Hotel","description":"","svgid":""}]}

我可以看到承诺最终在路由中得到了解决,数据也得到了正确的返回。然而,当我尝试点击select2控件时,我在控制台中收到以下错误:

Uncaught Error: Assertion Failed: select2 has no content!

如果我在控制器中使用静态数据,它就会工作。我有一种感觉,因为在呈现select2组件时承诺没有得到解决,所以它失败了?内容变量似乎没有设置为使用promise?我可以尝试使用查询,但我不希望进行类型先行查找。我只想显示一个简单的下拉列表,其中包含多个选项和要删除的选项。

我是错过了什么,还是这是个bug?

编辑:这是我正在使用的模型(models/accommodation-type.js)

import DS from 'ember-data';
export default DS.Model.extend({
    text: DS.attr('string'),
    description: DS.attr('string'),
    svgid:DS.attr('string'),
});

Bah。。我想明白了。我做错了以下两件事:1.事实证明,setupController是在视图渲染后调用的,因此视图在渲染时无法访问数据。因此,通过返回model hook的承诺进行了更正。这将使视图在解析模型之前无法进行渲染。

import Ember from 'ember';
import DS from 'ember-data';
export default Ember.Route.extend({
model: function(params) {
    return Ember.RSVP.hash({
        listings: this.store.find('listing', params),
        accomodationTypes: this.store.find('accomodation-type')
    });

    },
});
  1. 将模板中的select-2组件更改为使用以下组件:

    {{select-2 placeholder="Filter by" searchEnabled=true allowClear=true multiple=true content=model.accomodationTypes cssClass="search-filter-dropdown"}}
    

它就像一个符咒!

最新更新