簡體   English   中英

在Ember.js中為模板渲染2個模型時出錯

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

我有一個模板希望我有2個組件代表不同的模型。 我需要為每個組件創建一個包含兩個數據的模型。 如果我只有一個模型一切正常,但是當我添加其他模型時,則在控制台中發生此錯誤。

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

包含組件的模板的路由是:

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

});

這是我的模板:

<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>

有誰知道為什么會發生這種錯誤,我該怎么做才能解決它?

this.store.find('event', 'featured')無效。

此外,不推薦使用find

使用最新的Ember Data,您應該使用store.query()

你可能會喜歡它

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

要么

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

您需要相應地調整適配器。 就像是:

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

  return this._super(query, modelName);
}

這應該生成一個類似http://..../events/featured的URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM