使用Ember

时间:2016-06-27 20:31:14

标签: ember.js error-handling ember-data

我有new-thing的路线,它的模板是表格。当此表单提交时,我在路由中调用createThing操作,并传入this

export default Ember.Route.extend({
  model() {
    return {};
  },

  actions: {
    createThing(thing) {
      let thingToSave = this.store.createRecord('thing', {
        name: thing.name,
        description: thing.description
      });

      thingToSave.save().then(function() {
         // happy path
      }).catch(function(reason) {
        // unhappy path
      });
    }
  }
});

在我的模板中,我有以下内容:

{{#each model.errors.name as |error|}}
  <div class="error">
    {{error.message}}
  </div>
{{/each}}

但有些东西并没有正确连线。我不清楚路线的模型最终是thing我试图保存,以便在bad path触发时呈现错误。

1 个答案:

答案 0 :(得分:3)

您的model()应该创建空白记录:

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

这会将表单字段绑定到模型对象。

然后您可以保存在路由中并响应HTTP请求的结果:

actions: {
  createThing() {
    this.get('model').save().then((thing) => {
      // happy path
    },
    (err) => {
      // unhappy path
    })
  }
}
相关问题