Skip to content

Instantly share code, notes, and snippets.

@hrysd
Last active December 23, 2015 19:19
Show Gist options
  • Save hrysd/6681422 to your computer and use it in GitHub Desktop.
Save hrysd/6681422 to your computer and use it in GitHub Desktop.
ember-data(beta2) で async: true な child record を create

DS.ManyArray#createRecordcreateRecord をもっていてそのままつくれる

DS.PromiseArray はもってなくて作れないので、ManyArray にしてあげてから作るのがベストの様子

ManyArray#createRecord 内部で owner, store, type を用意してくれる

App.Post = DS.Model.extend({
  comments: DS.hasMany('comment', {async: true})
});

App.Comment = DS.Model.extend({
  post: DS.belongsTo('post')
});

App.CommentsController = Ember.ArrayController.extend({
  actions: {
    create: function(data){
      this.get('model').then(function(comments){ // DS.PromiseArray
        var comment;
        comment = comments.createRecord(data); // DS.ManyArray
        comment.save().then(function(comment){
          // 成功
        }, function(xhr){
          // 失敗
        });
      });
    }
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment