gpt4 book ai didi

javascript - Ember.js Railscast #410 未定义不是函数 TypeError : at RandomRaffle. EntriesRoute.Ember.Route.extend.setupController

转载 作者:行者123 更新时间:2023-12-02 17:04:31 25 4
gpt4 key购买 nike

我正在关注 Ember.js Railscast 第 410 集。当我更改 router.js 文件时

RandomRaffle.EntriesRoute = Ember.Route.extend({
setupController: function (controller) {
controller.set('content', []);
}

});

到此

RandomRaffle.EntriesRoute = Ember.Route.extend({
setupController: function (controller) {
controller.set('content', RandomRaffle.Entry.find());
}

});

我收到错误:

处理路由时出错:未定义的条目不是函数类型错误:未定义不是 RandomRaffle.EntriesRoute.Ember.Route.extend.setupController 的函数

我的 models/entry.js 文件包含:

RandomRaffle.Entry = DS.Model.extend({
name: DS.attr('string'),
winner: DS.attr('boolean')
});

我的controllers/entries_controller.js包含:

RandomRaffle.EntriesController = Ember.ArrayController.extend({
// newEntryName: "",

actions: {

addEntry: function () {
RandomRaffle.Entry.createRecord({name: this.get('newEntryName')});
this.set('newEntryName', "");
},

drawWinner: function () {
this.setEach('highlight', false);
var pool = this.rejectBy('winner');
if (pool.length > 0){
var entry = pool[Math.floor(Math.random() * pool.length)];
entry.set('winner', true);
entry.set('highlight', true);
this.get('store').commit();
}
}

}
});

javascripts/store.js

RandomRaffle.Store = DS.Store.extend({});

// Override the default adapter with the `DS.ActiveModelAdapter` which
// is built to work nicely with the ActiveModel::Serializers gem.
RandomRaffle.ApplicationAdapter = DS.ActiveModelAdapter.extend({});

我错过了什么?

最佳答案

如果您的store已正确定义,您应该创建一个 model route 中的函数,类似于:

RandomRaffle.EntriesRoute = Ember.Route.extend({
model: function() {
return RandomRaffle.Entry.find();

// This is deprecated. Latest versions of ember-data use the following:
// return this.store.find('entry');

// this method returns a promise that will
// carry the model data once it resolves
// This is internally passed as a param of `setupController`
},
setupController: function (controller, model) {

controller.set('content', model);
// this is the same as the default implementation, so if you're *NOT* doing
// anything different than this, get rid of `setupController` altogether

// the `model` param, is the return of the `model` function just above.
// It is returned a `promise` rather than data, and will be made
// available once the promise is resolved, materializing the records

// the param doesn't have to be called `model`, but the
// function above *HAS* to be named `model`
}

});

我不是 100% 确定,但我相信可能会发生错误,因为您在调用 RandomRaffle.Entry.find() 时应该是 this.store.find('条目')

关于javascript - Ember.js Railscast #410 未定义不是函数 TypeError : at RandomRaffle. EntriesRoute.Ember.Route.extend.setupController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25387634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com