Ember.js Railscast#410 undefined不是函数TypeError:在RandomRaffle.EntriesRoute.Ember.Route.extend.setupController

时间:2014-08-19 15:42:20

标签: javascript ember.js ember-data

我正在关注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());
  }

});

我收到错误:

处理路径时出错:条目undefined不是函数TypeError:undefined不是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();
      }
    }

  }
});

Javascript角/ 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({});

我错过了什么?

2 个答案:

答案 0 :(得分:2)

如果您的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` 
  }

});

我不是百分百肯定,但我相信错误可能正在发生,因为您应该RandomRaffle.Entry.find()时调用this.store.find('entry')

答案 1 :(得分:0)

models / entry.js和store.js文件在原始问题中是正确的。加...

router.js

RandomRaffler.EntriesRoute = Ember.Route.extend({

    model: function() {
       return this.store.find('entry');
   }
});

entries_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();           
    },

此外,您可能会在rails服务器日志中注意到csrf令牌存在问题,可以通过将此代码放在rails控制器文件的前几行(例如entries_controller.rb)来解决此问题。

skip_before_action :verify_authenticity_token

可以在https://github.com/markwiggles/raffler

找到Railscast项目的完整工作应用