【问题标题】:Ember and Yeoman error on edit controller编辑控制器上的 Ember 和 Yeoman 错误
【发布时间】:2014-02-20 02:26:23
【问题描述】:

按照post,在使用 Yeoman 和 Ember 创建工作流时,当我点击 /#/story/new url 我得到这个错误:

Error while loading route: Getbookmarks.StoryEditRoute<.model@http://localhost:9000/scripts/combined-scripts.js:117
superWrapper@http://localhost:9000/bower_components/ember/ember.js:1230
getModel@http://localhost:9000/bower_components/ember/ember.js:33281
model@http://localhost:9000/bower_components/ember/ember.js:33209
invokeCallback@http://localhost:9000/bower_components/ember/ember.js:9427
publish@http://localhost:9000/bower_components/ember/ember.js:9097
publishFulfillment@http://localhost:9000/bower_components/ember/ember.js:9517
DeferredActionQueues.prototype.flush@http://localhost:9000/bower_components/ember/ember.js:5650
Backburner.prototype.end@http://localhost:9000/bower_components/ember/ember.js:5741
Backburner.prototype.run@http://localhost:9000/bower_components/ember/ember.js:5780
Ember.run@http://localhost:9000/bower_components/ember/ember.js:6181
runInitialize@http://localhost:9000/bower_components/ember/ember.js:38453
jQuery.Callbacks/fire@http://localhost:9000/bower_components/jquery/jquery.js:2913
jQuery.Callbacks/self.fireWith@http://localhost:9000/bower_components/jquery/jquery.js:3025
.ready@http://localhost:9000/bower_components/jquery/jquery.js:398
completed@http://localhost:9000/bower_components/jquery/jquery.js:93

http://localhost:9000/bower_components/ember-data/ember-data.js
Line 3285

错误的详细信息是:

error(error=TypeError: this.modelFor(...) is undefined


return this.get('store').find('story', this.modelFor('story').id);

, transition=Object { router={...}, promise=Promise, data={...}, more...}, originRoute=<Getbookmarks.StoryEditRoute:ember265> { routeName="story_edit", router=<Getbookmarks.Router:ember266>, store=<Getbookmarks.Store:ember267>, more...})ember.js (line 33949)
triggerEvent(handlerInfos=[Object { isDynamic=false, name="application", handler=<Getbookmarks.ApplicationRoute:ember268>, more...}, Object { isDynamic=false, name="story_edit", handler=<Getbookmarks.StoryEditRoute:ember265>}], ignoreFailure=true, args=[
TypeError: this.modelFor(...) is undefined

我不知道我为什么会得到这个。我已经按照教程进行,我没有跳过任何内容。我担心的是教程的版本有点旧,但是代码是从 ember 生成器生成的。

有什么想法吗?

编辑:

这是为模型生成的代码:

Getbookmarks.StoryEditRoute = Ember.Route.extend({ 模型:函数(参数){ return this.get('store').find('story', this.modelFor('story').id); }, 设置控制器:功能(控制器,模型){ 控制器.set('模型',模型); buffer = model.get('attributes').map(function(attr){ 返回{键:attr.get('key'),值:attr.get('value')} }); 控制器.set('缓冲区',缓冲区) } });

路线: Getbookmarks.Router.map(function () {

this.resource('index',{path : '/'}); this.resource('story', { path: '/story/:story_id' }); this.resource('story_edit', { path: '/story/new' });

});

【问题讨论】:

  • 你能把你路由器的代码贴在这里吗?

标签: ember.js yeoman


【解决方案1】:

我昨天按照相同的指南遇到了同样的问题。这似乎是路线的问题,因为故事编辑需要一个story_id,我们不能为新故事提供它。我设法像这样解决了这个问题,为没有story_id的story_new添加了一条新路线:

在 routes.js 中:

this.resource('story_edit', { path: '/story/edit/:story_id' });
this.resource('story_new', { path: '/story/new' });

然后清空 StoryEditController(否则应用会抱怨“needs: 'story'”,除此之外无论如何它都会被替换)并将其添加到 routes/story_new_route.js 中:

Getbookmarks.StoryNewRoute = Ember.Route.extend({
  renderTemplate: function() {
    var controller = this.controllerFor('storyEdit');

    this.render('storyEdit', { controller: controller } );
  }
});

这个新控制器只会重定向到 storyEdit 控制器并使用 storyEdit-Template。然后清空story_edit_route.js(我们现在将使用默认值),您的应用程序应该会运行。

希望这也适用于您,否则请告诉我,我可以提供更多详细信息。

【讨论】:

  • welp 这并没有呈现新的东西,但至少我不再有这个烦人的错误了。我想知道,生成器中是否缺少一些代码?这是一个错误还是什么?无论如何,非常感谢
  • 对我来说,这看起来像是教程和生成器之间的版本不兼容。
猜你喜欢
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多