简体   繁体   English

在Ember.js中为模板渲染2个模型时出错

[英]Error when rendering 2 models for a template in Ember.js

I have a template in wish I have 2 components that represents different Models. 我有一个模板希望我有2个组件代表不同的模型。 I need to create a model that contains both datas, for each component. 我需要为每个组件创建一个包含两个数据的模型。 If I have only one model everything works fine, but when I add other one, then this error happens in the console. 如果我只有一个模型一切正常,但是当我添加其他模型时,则在控制台中发生此错误。

Error while processing route: events record is undefined ember$data$lib$system$store$finders$$_find/</<@http://localhost:1361/test/frontend/bower_components/ember-data/ember-data.prod.js:7475:11
Backburner.prototype.run@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:222:18
ember$data$lib$system$store$$Store<._adapterRun@http://localhost:1361/test/frontend/bower_components/ember-data/ember-data.prod.js:13133:16
ember$data$lib$system$store$finders$$_find/<@http://localhost:1361/test/frontend/bower_components/ember-data/ember-data.prod.js:7470:1
tryCatch@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:53070:14
invokeCallback@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:53085:15
publish@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:53053:9
@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:31253:7
Queue.prototype.invoke@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:901:9
Queue.prototype.flush@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:965:11
DeferredActionQueues.prototype.flush@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:765:11
Backburner.prototype.end@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:158:9
Backburner.prototype.run@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:226:13
run@http://localhost:1361/test/frontend/bower_components/ember/ember.prod.js:19151:12
ember$data$lib$adapters$rest$adapter$$RestAdapter<.ajax/</hash.success@http://localhost:1361/test/frontend/bower_components/ember-data/ember-data.prod.js:1728:15
n.Callbacks/j@http://localhost:1361/test/frontend/bower_components/jquery/dist/jquery.min.js:2:26920
n.Callbacks/k.fireWith@http://localhost:1361/test/frontend/bower_components/jquery/dist/jquery.min.js:2:27738
x@http://localhost:1361/test/frontend/bower_components/jquery/dist/jquery.min.js:4:11251
.send/b/<@http://localhost:1361/test/frontend/bower_components/jquery/dist/jquery.min.js:4:14765

The route of the template that contains the components is: 包含组件的模板的路由是:

App.EventsRoute = Ember.Route.extend({
    model: function()
    {
        return Ember.RSVP.hash({
            event:  this.store.find('event'),
            featured: this.store.find('event', 'featured')
        });
    }

});

Here is my template: 这是我的模板:

<script type="text/x-handlebars" id="events">
    <div class="col-xs-8 pull-left">
    {{#each event as |event|}}
        {{#event-box event=event}}{{/event-box}}
    {{else}}
    no events
    {{/each}}
    ...
            {{#each featured as |highlight|}}
                {{#highlight-event hlevent=highlight}}
                {{/highlight-event}}
            {{else}}
            No Highlights
            {{/each}}
           ...

</script>

Does anyone knows why this error happens and what can I do to solve it? 有谁知道为什么会发生这种错误,我该怎么做才能解决它?

this.store.find('event', 'featured') is invalid. this.store.find('event', 'featured')无效。

Also, find is deprecated. 此外,不推荐使用find

With the latest Ember Data, you should use store.query() . 使用最新的Ember Data,您应该使用store.query()

You'd probably use it like 你可能会喜欢它

this.store.query('event', {featured: true})

or 要么

this.store.query('event', {filter: 'featured'})

You'll need to adjust your adapter accordingly. 您需要相应地调整适配器。 Something like: 就像是:

urlForQuery: function(query, modelName) {
  if (query && query.featured === true) {
    delete query.featured;
    return this._buildURL(modelName, 'featured');
  }

  return this._super(query, modelName);
}

This should generate an URL like http://..../events/featured . 这应该生成一个类似http://..../events/featured的URL。

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

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