dimanche 25 février 2018

How to save a record and its model relationships

In a blog application, a user writes a post and selects some tags for it. When the user clicks Publish, it executes the publish action which currenlty does the following but its obviously not right because, while the post saves, the tags do not. No error is thrown in the console though:

actions: {
  publishPost: function() {
    var post = this.store.createRecord('post', {
      title: this.get('title'),
      body:  this.get('body')
    });
    post.set('tags', this.get('newTags')); // array of tag model objects (post hasMany tags in the model file)
    post.save();
  }
}

I've also tried just directly adding tags: this.get('newTags') to the 'post' object:

actions: {
  publishPost: function() {
    var post = this.store.createRecord('post', {
      title: this.get('title'),
      body:  this.get('body'),
      tags:  this.get('newTags')
    });
    post.save();
  }
}

How is this usually handled in Ember?




Aucun commentaire:

Enregistrer un commentaire