Alice goes to the forum and starts a new thread; she is presented with two input boxes, one for the title (of her thread) and one for the content (of the first post of the thread). When she’s happy, she hits the “post!” button for her thread to go live.
On the background, the ember squirrel has some work to do (in the save action of the NewThread route):
create a thread record
create a post record
assign the newly created post record to the hasMany relationship of the thread record
assign the newly created thread record to the belongsTo relationship of the post record
save the thread record, sending it over the wire to the datastore in the backend
save the post record as well.
My question is: how to code this list of steps in the save action? Though I feel I have a fair understanding of what needs to be done, I fail to see how ember wants this to be coded without fighting the framework.
Save the thread record so now you have the id assigned by the server
Assign the thread record to the belongsTo relationship of the post record
Save the post record so now you have the id assigned by the backend
Assign the post record to the hasMany relationship of the thread record
Here is some code:
...
save: function() {
// create the records
var thread = this.store.createRecord('thread', { title: this.get('tittle') });
var post = this.store.createRecord('post', { content: this.get('content') });
var _this = this;
// save the thread
thread.save().then(function() {
// now thread has the id
post.set('thread', thread);
// save the post
post.save().then(function() {
// the hasMany relationship is defined as `async: true` so you
// have to use `.then()` to access it because `thread.get('posts')`
// returns a promise
thread.get('posts').then(function(posts) {
// add the post to the thread.posts relationship
posts.pushObject(post);
// I think the following line is not necessary because
// thread.posts property is not saved on the server. If you do
// a this.store.find('thread', ...) to find this thread you get
// the posts filled with the saved posts that belong to it
thread.save();
// finally you can transition or do anything you want after the process
_this.transitionToRoute('thread.view', thread); // or whatever...
});
});
});
}
...
I’m new to Ember so I don’t know if this code or aproach is correct so any correction is welcome.
What I was missing, that you point out in your comments in the code, is that you need your backend on the server to assign an id to an object before you can stick that object as “participating” to whatever relationship.
You write:
I think the following line is not necessary because thread.posts property is not saved on the server […]
can you elaborate on that? Do you mean that relationships only “live” on the client, and are never sent to the server?
As you have a thread with many posts you may have a threads table and a posts table. The posts table should have a foreign key named thread (or thread_id) but you don’t have a posts key in the threads table. This is the reason to save the post after creating the thread (so you have the thread’s id) and not need to save the thread after including the post on the hasMany property.
As you say this relationship lives on the client and when you make a this.store.find('thread') is when you receive from the backend the posts ids that belong to each thread.
I don’t know if I have explained it well but I’m on vacancies without a computer and it’s difficult to me to write on the mobile