AJAX & Prototype Page 2 - Google's Closure Compiler Service API: Delivering Compiled Code in Different Formats |
As you may already have guessed, delivering compiled JavaScript code in XML is a process reduced to assigning the value “xml” to the “output_format” argument taken by the Closure Compiler Service API and nothing else. Below I've included a brand new code snippet to drive this point home. It shows how to convert optimized JavaScript code to XML by tweaking the aforementioned argument. Check it out: <?php // example using output_format = xml // create an instance of the file handler class and read the specified JavaScript file <compilationResult> Definitely, you’ll have to agree with me that the previous example is very easy to follow, right? As you can see, once the corresponding “output_format” argument has been passed to the API with a value of “xml,” the compiler has effectively returned the optimized code in XML format. Also, you should notice that the code has been wrapped inside of a couple of tags called <compilationResult> and <compiledCode>, so be careful of these elements, especially if you need to apply some form of additional processing to the output once it has been returned to the client. So far, so good. Having explained in detail how make the Closure Compiler Service API return optimized code in XML, the next step is to discuss how to instruct the compiler to deliver the processed output in JSON format. Still not sure how to achieve this? Well, to find the answer, read the following lines. Going one step further: serving compiled code in JSON format The only thing we need to do to return compiled code in JSON format is assign the value “json” to the “output_format” argument accepted by the Closure Compiler Service API. To shed some light on how to achieve this, I included an amended version of the example created in the previous segment, which this time sends the optimized JavaScript back to the client as a JSON object. Here it is: <?php // example using output_format = json // create an instance of the file handler class and read the specified JavaScript file As the above snippet shows, the assignment of the value “json” to the “output_format” option has obligated the compiler to return the optimized JavaScript in the form of a simple JSON object, whose key is identified as “compiledCode,” and its value is not surprisingly the optimized output itself. That was pretty simple to understand, wasn’t it? While the examples that you just saw demonstrate that the Closure Compiler Service API yields quite satisfactory results when it comes to delivering compiled JavaScript in different formats, most likely you’ll be using the default “text/javascript” type. Nevertheless, if you're used to building web applications that deal with XML or JSON encoded data, you might find the options discussed before pretty helpful. Final thoughts That’s all for the moment. Over the course of this sixth part of the series, I explored another useful feature provided by Google's Closure Compiler Service API, which through its “output_format” argument permits you to deliver compiled JavaScript code to the client in different formats, included the already familiar “text/javascript” type, then XML and finally JSON. Needless to say, it’s possible to return optimized code in formats other than the ones mentioned above (for instance, you might want to embed the compiled output into a PDF document for display purposes). In a case like this, you’ll have to first grab the corresponding output and generate the PDF file by using a third-party library like PDFlib (http://pecl.php.net/package/pdflib), or a similar package. Possibilities are endless. Moving on, it’s time to speak of the topics that I plan to discuss in the upcoming episode. As you may have already noticed in all of the examples developed so far, the Closure Compiler Service API takes an argument called “output_info,” which is used to tell the compiler what kind of output must be returned to the client. In all of the aforementioned examples, the value assigned to this argument was “'compiled_code,” which makes the compiler deliver exactly that: the compiled JavaScript. It’s feasible, however, to return distinct information, such as the errors and warnings generated during the compiling process by tweaking the argument in question. Given that possibility, in the next tutorial I’m going to build a few additional examples that will show how to play with the “output_info” parameter in a truly painless fashion. Here’s my final piece of advice: don’t miss the next part!
blog comments powered by Disqus |
|
|
|
|
|
|
|