So Jacob, the problem here is just that before rendering your view, you need to wait the response from the server. Your this.model.id
will be undefined
because your model is new, that means that it was not saved in the server yet.
So, what you could do is to work with the success
callback from the .create
function. I believe that something like this would work for you.
if (this.model.isNew()) {
collection.create({
// or you can use this.model (if it is an object)
attribute_x: 'val',
attribute_y: 'val'
}, success: function(response){
// here your model with have an id
console.log(response);
new MyView({
model: response.model // check your response
}).render().appendNode();
});
} else {
new MyView({
model: this.model
}).render().appendNode();
}
So, as you can see, in the success
callback, we render the View
. And if you console.log
the response you'll see what your back-end responded to you in the creation of the model.