Lessons: 10Length: 59 minutes

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Getting More Data From Your Falcor Model

Since we now know how to use a Falcor model to retrieve a single piece of information from our JSON data, wouldn't it be nice to be able to retrieve multiple pieces of information, whether or not they are found in the same location as your JSON data? Of course it would. In this lesson, I'll teach you to do just that.

2.3 Getting More Data From Your Falcor Model

So far we've been able to see how we can retrieve a single piece of information from a rather simple JSON object. Even though that could get infinitely more complex over time, but that seems a little bit limiting. How do we take advantage of the same style of syntax where we can reach into our model and get a value using a specific location within our model? How do we maybe get additional pieces of information or multiple pieces of information? How do I get maybe the names from all the to-dos of everything that's found within this to-do list. Or maybe how do I get a range of data? All the names from the first to-do item in all of the to-do lists or things like that. How can we start to extend the reach of our Falcor model and it's actually quite simple. So Falcor extends JSON a little bit to take some JavaScript pieces of functionality and injects it into JSON. So that we can start to retrieve things in a very common manner. And especially if you've been doing a little bit of client side development with JavaScript or jQuery and JSON that you're probably already familiar with. And it's actually quite easy to start to get into. So let's say we wanted to retrieve the first piece of, the first todos name from all of the To Do lists. So, in this case, there's only two, but how would I retrieve all of them? Well, one way that I could do that is by simply specifying within my path into the JSON object. I can specify a range when it comes to a raise. So in this case, I could say instead of saying I want just one or the first to-do list, I can actually specify arrange. And say I want 0 up to 1 inclusive and I want all of those names of the first todo item in all of the lists. So in this case, I would actually get two back. I would get the Watch Tuts Videos and Get a great job but things change a little bit. So if I just try to save this and I hop over to my browser, I'm gonna go ahead try to run this. But as you see here, I'm gonna get an error that paths must be simple paths. And the reason for that is because once you start to ask for a collection of information or multiple, in this case, I'm actually asking for a key. I'm reaching into my JSON object and I want the name key found at this particular location, which ends up being right here. Once I start asking for multiple keys, I have to use the get function as opposed to getValue. And then things are gonna change a little bit. So I'm not actually just getting back an individual value now. I'm actually getting a subset of my actual model. So I'm going to be getting back more than just that individual piece. So let's go ahead and save that, and let's come back here and run this again. As you see here I'm actually getting a JSON object. So let's expand this a little bit, I see JSON. I see todoList, so that was definitely within my model. And then I can come in here, I see two objects. I see 0 and 1 and I see todos. So I see my todos list in each one. And I get an individual object, I see Watch Tuts Videos. And if I were to look into the second one, I see Get a great job. So this is actually doing things a little bit differently than you might imagine. Instead of just reaching in and grabbing an individual property value and sending back maybe an array of those individual values, we're actually getting the full JSON structure. With only the pieces of information filled in that we actually asked for. Which is actually kind of interesting because, like I said, we're not getting an array of pieces of data. We're actually starting to see the JSON object unfold before us, but we're only getting what we asked for. But we don't have to stop there. We could do this range of data on all of these arrays. So I can say do the same thing for the todos. I can ask for zero to one, even though there's only one in each one of these. But what if I wanted to get multiple pieces of data? What if I didn't only want to get the name, I also wanted to know whether it was completed or not? Well, I could start by passing in actually a comma delimited list of the things that I'm asking for. So I can say give me all the names that are found within there, as well as giving me all of the completed flags. So I could go ahead and save that. I'll come back over here and refresh. And once again, I get this object, so I could begin to drill down in here a little bit. But to make our lives a little bit easier, I'm gonna modify the console.log down here. So instead of just doing this value, I don't actually want to do that. Cuz what this is is actually a JSON object. So I'm gonna change this and say, JSON and then I don't wanna just log that value. I would actually like to stringify that value. So I wanna stringify that JSON. I don't wanna pass in any replacers. And let's do two spaces. So let's go ahead and save that. We'll come back over here and we'll refresh. And now we can see a little bit more data. So let's go ahead and expand this a little bit. So as you can see here, we're getting back a full JSON object. Here's our todoList. There are two pieces of data within that todo list. One at index zero. One at index one. And then I have the todos collection underneath it. And then within there, I see name and completed for both of those things. So that's pretty nice. I can start to request multiple pieces of information. But as you can imagine, this is gonna get very cumbersome after a while. Can we shorten this up a little bit? Well, we absolutely can. Now what's different between these two things that we're passing into our model to say retrieve these pieces of information? Well, the only things that are different are name and completed. So wouldn't it be nice if we could combine all of this and just have the differences be mentioned as what we're trying to request? And actually we can do that. All we have to do is instead of doing dot notation here, we're going to do an open and closed square brackets. And then within here, we are going to simply specify the names of the properties that we want in here. So I can do it just like this. I no longer need the second parameter, which I could leave in there if I wanted to do it that way. But now I've kind of condensed this down a little bit more. And if I were to come back in here and rerun this, you're gonna see I get the same result. So, this is becoming rather interesting very quickly in that, I can now be very specific and pointed about an individual item that I want to get. But I can also be a little bit broader and say I want to get a series of properties for this particular model that fall within this path. Or that looks where the paths match up relatively with this by using these different variations of the syntax. Now, there's lots of different ways that you can specify these in shorthandese a little bit more and do some more interesting little tricks. And I'm gonna provide a little bit more information for you on how to do some of those things at the end of the course. But this should really be able to get you up and running fairly quickly on how to get multiple values back from your Falcor model.

Back to the top