emberjs / guides

This repository is DEPRECATED!
https://github.com/ember-learn/guides-source
Other
283 stars 873 forks source link

Examples for creating Records #2256

Closed kle-dev closed 6 years ago

kle-dev commented 6 years ago

written at https://guides.emberjs.com/v3.0.0/models/relationships/ this is not working:

let blogPost = this.get('store').peekRecord('blog-post', 1);
let comment = this.get('store').createRecord('comment', {
});
blogPost.get('comments').pushObject(comment);
comment.save().then(function () {
  blogPost.save();
}); 

With: TypeError: blogPost.get(...).pushObject is not a function

the following example also didnt work for me also when the record is saved:

For example, if you want to set the author property of a blogPost, this would not work if the user with id isn't already loaded into the store:

this.get('store').createRecord('blog-post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum',
  author: this.get('store').findRecord('user', 1)
});

I think that there should be at least an .save() at the end for a full example and for users who want to copy and paste and try it out.

I also find out that the following doesn't work

let blogPost = this.get('store').peekRecord('blog-post', 1);
let comment = this.get('store').createRecord('comment', {
  blogPost: blogPost
});
comment.save();

=================================== there aren't any examples for users who want to create her relationships like an working example:

  let adress = this.get('store').createRecord("customer", {...});
  let customer = this.get('store').createRecord("customer", {...}).save().then((_customer) => {
            this.get('store').findRecord('customer', _customer.id).then(function (recordCustomer) {
               recordCustomer('adress', adress);
               recordCustomer().then(() => {
                   adress.save();
               });

by the way.. my question: is that the only way to do that ?

Thanks