July 08, 2014

JavaScript Variable Hoisting

One of the little known nuances of JavaScript, is that the interpreter organizes functions and variables per scope by declaring them at the beginning of the scoping container, regardless of their order in the written script. Here's an example:

Displays:

x=undefined
x=1
Uncaught ReferenceError: y is not defined
In the above example, we're logging the value of variables x and y before they are declared, however, x has a value of
undefined
whereas y throws an error. So what's the difference? Neither variable has been declared when we are accessing them. Why do we only get an error when referencing y?

The reason is that the JavaScript interpreter reorganizes the declaration of ALL varibles within a scope to the top of the scope - in our case our scope is the function
showVariableValues()
. So in reality, our above code will be reorganized as shown below:

Notice that the declaration of var x has been "hoisted" to the top of the scope, but only the declaration. The initialization of the variable remains in its original spot. Once this variable was declared, JavaScript set its value to
undefined
- the JavaScript default. So that's why when we write the value of x, it's value is
undefined
, whereas y just throws an error because it has not been declared.

So the lesson is: always declare your variables at the beginning of their scope - if you don't, JavaScript will reorganize your code for you. If you think about it, you could probably come up with scenarios with much more confusing results than this simple example.

Share |

February 26, 2014

This Fine Morning


I like V8 juice. Every morning, on the way into work, I drink a glass of V8 juice.


Today was a bit strange - we had some flurries overnight, and I had left my car outside, so it had a coating of snow, so I took a few minutes and brushed the snow off the windows before leaving.


Since I was running late, I had to take our daily Skype call via cell. Usually I do it from my laptop at my desk, but in a pinch like this, I had to take it on my Android in the car.


But during the call, I noticed out of the corner of my eye that someone was gesturing to me from the car beside me...


After watching her for a few seconds, she then started gesturing like she was taking a drink, and pointing to my roof... and it hit me... I'd left my V8 on the roof of my car! Then, double-yikes, we were out of plastic cups, and I'd used a GLASS today! - Holy crap, there was a glass balancing on my roof driving 70 MPH on the interstate!


So the first thing I did was try to get as far ahead / away from everyone else as I could, and look for the first place I could C-A-R-E-F-U-L-L-Y pull over.


When I finally was able to pull over and come to a stop, I got out among some curious looks, and sure enough my glass of V8 was still precariously perched on my roof. Between the snow on my roof, the wetness of the glass, and the cold temperature, the glass had become soldered to my roof with ice. In fact, it was attached so firmly that I had to pry it off. I probably could have done donuts and would have only spilled the juice. Happy ending.

Share |

January 21, 2014

Using custom binding to manage large datasets with the Kendo Grid

The KendoUI grid comes with robust client-side functionality, and Ajax-capabilities - sorting, filtering, paging is all just built in. With the Razor wrapper, the .Pageable(), .Sortable(), and .Filterable() extension methods take care of it all. If the data you’re displaying is minimal or even a few thousand rows, this is probably the route to go – just set the grid's dataSource to Ajax, create a read action on your controller, and you’ve got a pretty robust grid.

But once you get into dealing with “large” datasets, you may want to consider implementing custom binding to get data in front of the user without the wait. When I say “large”, I mean data that takes more than a few seconds to display, or data that is likely grow quickly to an unmanageable size. In our scenario the size of the dataset could be up to 25 Mb, but even after compressing it to 800k, it was taking several seconds to send the data from the server to the browser and then display the data in the grid. After the painful initial wait though, paging, sorting, filtering was then instantaneous since the data was all client side. But what to do about the initial wait?

That’s where custom binding can help out. By implementing custom binding, you can combine the best of Ajax and Server binding to quickly deliver large datasets to your users. A small drawback is that all the sorting, filtering, paging has to be handled in the controller method. Fortunately Kendo makes this fairly easy by automatically passing collections of criteria on the request object – here’s a quick overview of server code required to manually implement paging, sorting and filtering when implementing custom binding.

Sorting
The sorts collection on the request object contains the fields and directions of all sorts that are currently applied to the grid.

Filtering
This is beautiful – I was expecting to have to write code to handle all possible operators and fields, but Kendo’s ExpressionBuilder class will take the filters collection from the request and convert it to a lambda expression to be applied to your dataset! Way easy.

Paging
Based on the page number and the page size, determine the subset of data to return to the client.

One other thing to remember when implementing custom binding is that Server caching of the dataset is something to keep in mind – especially if the db call takes more than a second or two. With custom binding, we’re calling back to the server on every filter, page, or sort event – so we don’t want to make a db call on top of that every time. This is true even (and especially) if we use Kendo’s Server databinding instead of Ajax databinding. So anyway, if you’re using Kendo’s data grids, the out of the box Ajax binding is a good, easily implementable solution, but if you’re dealing with large amounts of data, you’ll want to look into custom binding.

Full Razor code for grid with custom binding enabled:



Full controller method for grid Read event:

Share |