Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:24Length:1.8 hours
Backbone
  • Overview
  • Transcript

2.5 Using Model IDs

We saw in the last lesson that its important for our server backend here to apply an ID attribute to our models when they're being saved. If an ID is not added when its being saved, then later on when the user tries to update that model, Backbone won't realize that the model has been previously saved, because it's looking for an attribute named ID, and so it will try and save that as a new model, instead of doing the appropriate put request. The ID of course is also used with the is new method and in a couple of other ways too when say we're destroying the model or doing a fetch to read the model information from the server. However it's possible that you'll have a backend where the ID is not actually named ID which is what we did here. And Backbone does by default expect it do be called id. So let's change the name of ID here. So we have to change it in two places. Of course where it's created in our post here, we apply an ID called book and for this example, we'll just use _id. This is something that is used within, for example, MongoDB. It uses _id. And so if you have a MongoDB background, _id might be what you have to work with, and Backbone can work with this, but we just have to change it up a little. I'll also add an underscore up here, where we create our one default record in our database. Okay. So now let's come back to the client side here. And so now let's create a new book. And now we'll go ahead and do book.save. And what I should have done before this is show you book.isNew, but you know that isnew would have been true in that case. You can see here we've made our post request, and as you can see, it's come back here, and we have our _id attribute added to our model. So now if I do book.isNew, it's still considered to be new, because it doesn't have a property named id. And so at this point, if I do book.set, published, now if we do book.save, if we go to the Network tab here, you can see that another post request was done, and we actually have a whole new model with another id, id 3 instead of id 2. And the problem here is that our server is doing everything right, it's just not using the id attribute name that Backbone is expecting. And so what we need to do is come over here to where we create our model, and we have to add an id attribute property. And let's just set this to _id, excellent. So now I will comeback and refresh our server here, and let's create our new book. And now, I'll do book.save, and if you look at the network tab here, you can see that as you might expect, we have our _id applied. But now, if I do book.isNew, you can see false, because this model class, book, now knows what type of attribute it's looking for. Let's try book.save, and we'll do an update by adding a published date. And now if we look at the Network tab, you can see that right here, we actually have a put request, which was updating our current model instead of creating a new one. And you can actually see the id in place here. If we look at the book you can see that the model itself gets an id property with whatever that id value is. Inside attributes we of course have the id as it is named on the server, so _id. And one more thing, you might also notice this cid or client side id attribute. Every backbone object that we create gets a client side id which is a handy way to determine model uniqueness if you have a set of models that don't ever actually go to the server. So you can use the client id to determine which one is which. And so that ID attribute property is something you need your models to be aware of if your server side doesn't use the default id attribute that Backbone expects.

Back to the top