Delayed Client Loading with WebSync and jQuery

by jerod.venema 17. August 2010 02:15

Warning: technical content ahead!

Recently, we were recently doing some work for a client when we came across a minor problem. We were building an application that had a bit of a unique requirement: the entire application had to work with the inclusion of a single <script> tag. Scripts could be added dynamically, but the end user had to only see a single tag.

"Simple enough", we thought; we'll simply combine the JS and CSS server-side, and be done with it. Of course, that would have meant manually extracting the JavaScript for the WebSync client from the server code. Now, that's not difficult to do (it's just an embedded resource), but we were curious if there was an even simpler solution. An extra request was completely acceptable, and since we were already using jQuery, we figured we'd just use that to load the script. The result was this:

$(function(){
  $.getScript("/client.ashx", function(){
    client.initialize();
    client.connect();
    // ...etc
  });
});

"Great", we thought. Problem solved. But then we realized that none of our client requests were being sent anymore! We pondered for a moment, and then remembered that the WebSync client is designed to monitor the "contentReady" event of the DOM so as to avoid the dreaded never-ending loading indicator. So, if the client script loads after the DOM event has already fired, the client is never notified that it is allowed to start making requests.

Thankfully, the designers of WebSync (that's us!) put some thought into this problem, and had a solution already waiting in the wings:

$(function(){
  $.getScript("/client.ashx", function(){
    fm.utilities.ready();
  });
});

That handy extra line tells the WebSync client that the DOM is ready to be used. It then fires any events that were queued up in the addOnLoad() utility function, running them immediately. That's it! So what're you waiting for - get coding!

Tags: , , ,

jquery | websync

WebSync 3.1.0.1 Released!

by jerod.venema 6. August 2010 03:35

Hey everyone!

Minor point release today, but it does have a pretty important fix for the JavaScript client in Internet Explorer when the connection has a "soft" reset (like changing your web.config).

Please update to 3.1.0.1 if you're running WebSync 3.1! (You can check by right clicking on your file an selecting "Properties")

Customers: You can grab a copy from the "Portal" (http://www.frozenmountain.com/portal) on the "Order History" tab

Trial Users: You can grab a copy from the "Downloads" (http://www.frozenmountain.com/websync/downloads)

On-Demand Users: You don't need to do anything, this fix has been applied for you already :)

Happy coding!

Tags: