2.4 Updating Data
Getting data from a model, or an API, is really great when you're just getting started. But in the real world, data doesn't only flow in one direction—we often need to create and update data. In this lesson, I'll show you how to update your JSON data using the Falcor model.
1.Introduction2 lessons, 04:13
2.Getting to Know Falcor7 lessons, 52:40
3.Conclusion1 lesson, 02:31
2.4 Updating Data
So far, we have learned how we can retrieve a single piece of information out of our model. We now can retrieve multiple properties within our model and that's all really fine and good, but what if we wanted to update or set some fields or set some values within our model how can we do that. Well it's very simple just as the gets are, we're gonna use some similar functions or a similar function to our get value that we used before. But this time we're gonna use something called set value. So the way that we do that is we once again reach into our model, and this time we're gonna say set value. And once again we are gonna refer to a specific location within our model that we want to update. So let's just go ahead and copy this and modify it ever so slightly. So let's say that within my recently viewed to do list, I want to mark the first item that's found in here which happens to be watched Tuts videos. I want to mark it completed as true. So the way that we're gonna do that is we're gonna refer to the first to do list. The first item within that to do list. And I want to set the name property. So that's the actual location that I want to. Or, excuse me. Not the name property. The completed property. And I want to set that value to true. Just like that. So it's pretty simple and once again this is all done asynchronously, so we're gonna get a promise back for that which would be then we can reach we can do something with what's returned, in the event function but in this case instead of really doing anything with that, all I wanna do, is I want to go ahead and retrieve the updated models, so we can see that something has actually changed. So as you can see here now, we're gonna change a lot of these promises together which as I mentioned before is one of the more powerful features of switching from a synchronous model to an asynchronous model using these promises we can now chain these things again gather and say once you're done with that, do this and then when you're done with that, do this we can start to just kind of chain all these things together which is pretty nice. So I wanna go ahead and set the completed value to true and then once that has completed I want to issue another get and I wanna once again get all the to do lists the first item the name and completed properties. So I would in this case be thinking I would be getting something very similar, to what I got before as you can see over here. This is the result that we had before we got watched his videos completed false and get a great job completed false. So if I were to refresh this now you're gonna see, I get Watch Tuts Videos completed true, and get a great job completed false. So you can see now, we are able to update data as well. Now one thing you might begin to ask yourself or to think about here is, what happens in the case where maybe I have something set up this way. Where maybe I've gone and as you can see here we've got this recently viewed to do list and a recently added list, what if this is kind of showing you that ultimately we can have similar values or similar items show up in multiple to do lists. So there's really no reason why I could go looking through my app and I could see if that the get a great job to do list item could show up in multiple places, right, it could be on multiple to do lists. So, if I were to say grab this, and copy this, and then we could put it over here within this to do list as well. So let's go ahead and save that. So now let's think about what would happen, if I went and ran this operation again. So this time I'm gonna set completed to true on the first thing again, which means it's gonna be updating now get a great job, and then it's gonna go and retrieve everything again. So after I've saved that, let's go ahead and rerun this. And you're gonna see here that I'm seeing get a great job, completed true. Get a great job, completed false. Now this brings up an interesting point, since we're talking technically about the same to do list item, because once again these things have the same ID, so truly specifically speaking, when it comes to the user that's looking at these items regardless of where they show up in whatever list, probably it could be argued that these things should have the same data. So just because, I have recently added a to do list item here and maybe I just looked at it. It showed up in this particular list, so it's really just a copy of the same data. If I were to update this piece of information, to be true then technically speaking if I looked at that. In another list somewhere like it recently added it should probably be true as well. So we're starting to introduce this concept of stale data and that's a very big problem that you can run into in something like this when you start to deal with models and depending on how they're stored on the back-end within your API. Now, very interestingly enough this has been a very big problem that many people many developers have had to deal with over the years. And Netflix has come up with a very interesting solution to this problem, which they are calling the Json graph, which allows us to restructure our model a little bit and be able to actually get rid of a lot of this duplication that shows up, typically within these Json models that you could see within your applications and that's what I'm gonna show you how to do in the next lesson.