[英]Ember.js Railscast #410 undefined is not a function TypeError: at RandomRaffle.EntriesRoute.Ember.Route.extend.setupController
I'm following the Ember.js Railscast episode 410. When I change my router.js file from 我正在关注Ember.js Railscast 410集。当我从以下位置更改router.js文件时
RandomRaffle.EntriesRoute = Ember.Route.extend({
setupController: function (controller) {
controller.set('content', []);
}
});
to this 对此
RandomRaffle.EntriesRoute = Ember.Route.extend({
setupController: function (controller) {
controller.set('content', RandomRaffle.Entry.find());
}
});
I get the error: 我得到错误:
Error while processing route: entries undefined is not a function TypeError: undefined is not a function at RandomRaffle.EntriesRoute.Ember.Route.extend.setupController 处理路线时出错:未定义的条目不是函数TypeError:在RandomRaffle.EntriesRoute.Ember.Route.extend.setupController上的未定义函数不是函数
My models/entry.js file contains: 我的models / entry.js文件包含:
RandomRaffle.Entry = DS.Model.extend({
name: DS.attr('string'),
winner: DS.attr('boolean')
});
My controllers/entries_controller.js contains: 我的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 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({});
What am I missing? 我想念什么?
Providing your store
is properly defined, you should create a model
function in your route
, similar to this: 如果正确定义了store
,则应在route
创建一个model
函数,类似于:
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`
}
});
I am not 100% sure, but I believe the error may be happening because you are calling RandomRaffle.Entry.find()
when it should be this.store.find('entry')
我不确定100%,但我认为可能会发生错误,因为您在应为this.store.find('entry')
时调用RandomRaffle.Entry.find()
this.store.find('entry')
The models/entry.js and store.js files are correct in the original question. 在原始问题中,models / entry.js和store.js文件是正确的。 Plus... 加...
router.js router.js
RandomRaffler.EntriesRoute = Ember.Route.extend({
model: function() {
return this.store.find('entry');
}
});
entries_controller.js entry_controller.js
RandomRaffler.EntriesController = Ember.ArrayController.extend({
actions: {
addEntry: function(name) {
//Create the entry record
var entry = this.store.createRecord('entry', {
name: name,
winner: false
});
// Save the changes
entry.save();
},
Also you may notice in your rails server log that there is a problem with the csrf token, this can be fixed by placing this code in the first few lines of the rails controller file (eg entries_controller.rb) 另外,您可能会在Rails服务器日志中注意到csrf令牌存在问题,可以通过将此代码放置在rails控制器文件的前几行中来解决此问题(例如,entrys_controller.rb)
skip_before_action :verify_authenticity_token
A full working app of the Railscast project can be found at https://github.com/markwiggles/raffler 可以在https://github.com/markwiggles/raffler上找到Railscast项目的完整工作应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.