Ember 数据在提交时未发送数据

Ember Data not sending data on submit

当我提交表单时,我收到以下回复:

{"data":{"attributes":{"title":null,"description":null},"type":"cards"}}

我不确定为什么 titledescription 为空。

routes/cards/new.js:

actions: {
  save() {
    const newCard = this.get('store').createRecord('card', this.get('model'));
    newCard.save().then((card) => {
      this.transitionTo('cards.all');
    });
  },
  cancel() {
    this.transitionTo('cards');
  }
}

templates/cards/new.hbs:

<form>
  <div>
    <label>Title:</label>
    {{input type="text" value=model.title}}
  </div>
  <div>
    <label>Body:</label>
    {{textarea rows="5" value=model.description}}
  </div>
  <div>
    <button {{action 'save'}}>Speichern</button>
    <button {{action 'cancel'}}>Abbrechen</button>
  </div>
</form>

回购 link:https://github.com/ghoshnirmalya/hub-client

您没有正确地将 .hbs 中的标题和描述传递给路线。在触发动作保存后,您正在创建模型。将 model.title 更改为 title 并对描述执行相同的操作。将它们传递到您的路线:{{ save title description }}。然后在保存操作中定义两个参数,如:save(title, description)。我相信你能想出剩下的。

这是我通常在我的路线中做的事情:

setupController(controller /*, model */ ) {
    this._super(...arguments);
    Ember.set(controller, 'newCard', {}); //newCard is an empty object
},
actions: {
    save(newCard) {
        Ember.assert('Model is missing or undefined', newCard);
        let newCard = this.store.createRecord('card', newCard);
        newCard.save().then(( /* response */ ) => {
            this.transitionTo('cards.all');
        }, (error) => {
            //handle error
        });
    }
}

在您的模板中,您可以这样做:

<form id="save" {{action "save" newCard on="submit"}}>
    {{input name="title" id="title" value=newCard.title type="text"}}
    <button class="button" type="submit">Save</button>
</form>

希望这对您有所帮助。杰夫

你在评论中提到

doing a console.log(this.get('model')) just prints the model function

这就是您问题的答案!因为在路线中你可能有模型挂钩功能。所以 this.get('model') 将 return 函数代替模型。 因此,为 cards/new.js 创建控制器,您可以移动现有的保存操作。这应该有效。