Ember嵌套路线

时间:2016-09-01 20:35:24

标签: ember.js ember-cli

我有一个模型工厂和相关的模型种植,应该有以下网址:

/plants
/plants/new
/plants/some-plant-id/planting/new  

添加种植的路线在网址中有效,并显示正确的添加表单。问题是提交与种植/新的行动不起作用。

控制台

ember.debug.js:19699 Uncaught Error: Nothing handled the action 'savePlanting'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.

我猜这个问题与我在router.js中构建路由的方式有关但不确定。只有一个控制器应用程序/控制器/ application.js是最小的

以下是我的一些代码

应用程序/路由/种植/ new.js

import Ember from 'ember';

export default Ember.Route.extend({

  model() {    
    return this.store.createRecord('planting');
  },

  actions: {

    savePlanting(newPlanting) {
      console.log("trying to save...");
      newPlanting.save().then(() => this.transitionTo('planting'));
    },

    willTransition() {
       //rollbackAttributes() removes the record from the store
       //if the model 'isNew'
      this.controller.get('model').rollbackAttributes();
    }
  }
});

router.js

...
this.route('plants', function() {
  this.route('new');
  this.route('edit', { path: '/:plant_id/edit' });

  this.route('planting',  { path: '/plants/:plant_id/planting' },   function(){
    this.route('new');
  });

});
...

顺便说一句,我的模板目录就像

app/templates/plants
app/templates/plants/new.hbs
app/templates/plants/planting/new.hbs  //plantings dir in plants

路由目录

app/routes/plants
app/routes/planting

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我认为您的问题是您希望使用模板中route的操作。

为此,您应该使用route-action助手(https://github.com/DockYard/ember-route-action-helper)。

否则它会在控制器中寻找一个动作:)

答案 1 :(得分:1)

如果您看到保存新种植的ajax请求转到后端,那么也可能会以某种方式将操作冒出来(这就是错误消息的第二部分所指的内容)。

你应该做的一件事是在动作处理程序newPlanting.save()中返回promise并查看是否有所改变。另外,请向我们展示触发操作的模板。

相关问题