Comments
litl_phil wrote: While it's nice that Google and Acer share the vision of cloud-based computing, it's also worth noting that we at litl already have a webbook on the market (available at litl.com) that runs our own cloud-based OS. Unlike Chrome, litlOS is focused on creating a new and better web experience for the home, so we don't have the usual browser interface, we have our own innovative UI. In conjunction with easel mode (litl's inverted-V position) and our growing cohort of litl channels (special apps t...
Cloud Expo on Google News


2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


AJAX Patterns: Introducing JavaScriptBeans – Bringing JavaBeans to JavaScript
In software engineering deja vu is called design pattern

Six month ago, Alex Iskold (pictured) switched from J2EE Grid Computing to Web 2.0, JavaScript and Firefox extension development. He has been writing in Web 2.0 Journal about his experiences - see "From J2EE to JavaScript".

Through this transition, I am continuing to have a feeling of deja vu. I write code thinking – 'Hey I've seen this before or I've done this before'. In software engineering deja vu is called 'design pattern.' If there is a good way to do something, we discover it, add it to the patterns catalog and apply it again and again.

I remember being very impressed with elegance and simplicity of JavaBeans framework. This was one of the early naming-convention based patterns introduced by Java designers. The bean pattern was simple, but very powerful; it showed that a simple naming convention can go a long way. Naming conventions fuel automation, lead to reduction of code and save time.  
 
Problem

The application that we are developing at adaptiveblue, is called blueorganizer. It is smart browsing extensions for Firefox, which brings semantics of everyday objects into the browser. Among key concepts of the blueorganizer is the concept of collection. The users are able to collect objects like  books, movies, wines, restaurants or cars from blogs and web pages.
 

Early on we realized that we need to design the blueorganizer in such a way that users do not have to reinstall the extension each time when we roll out new functionality. We were inspired by the way that pure web applications work – the new functionality can be rolled out literally overnight to everyone. We needed the same capability for many aspects of the blueorganizer. Essentially we needed a hybrid between a classic desktop application and a dynamic web site. We accomplished this by bringing JavaBeans to JavaScript.

 
JavaScriptBeans

The idea that we came up with was to have each collection defined in a separate file and loaded via XMLHttpRequest. We also have a master file in the well known location that would list all available collections. At startup, the collections list is fetched and then all collections are loaded one by one. We needed a mechanism for instantiating the collection from the XML file, and thats when we naturally thought about beans.
 

We looked around and found JSON. - a common way for serializing JavaScript objects. JSON is nice and quite popular. It is used for example, by del.icio.us API to stream responses directly to JavaScript clients. But we decided that it would not work for us because it is not easily readable. We wanted a solution where we can describe the configuration of the object using XML file, which would be read in at the run time to construct corresponding JavaScript objects.

 

Here is the simple example of what we had in mind as an input file:

<!--[if !vml]-->



<!--[endif]-->

This should construct JavaScript object of type BasicModel and to set a name property of this object to be 'Books'.

 
Mechanics
 
Recall that in Java the basic deserialization of beans from XML follows a pattern: a tag is processed and complex object is instantiated, then process recurses into children tags instantiating children objects and setting them as bean properties of the parent. When the tag specified a trivial property like integer or a double, the property editors where used as converters to convert the object from string to the specific data type. With JavaScript things are even simpler, since there is no need to convert primitive types.
 

To handle the deserialization we create BeanUtils.js with the following main interface:

 

<!--[if !vml]-->



<!--[endif]-->

That is, the function takes as an input the root element, parent object and the urlBase (we will explain this parameter in a bit) and returns the fully instantiated JavaScript object. Here is the simplified version of our implementation:

 

<!--[if !vml]-->



<!--[endif]-->

This is it in a nutshell, but there is one caveat – in order for eval() to work, the object that is being instantiated needs to be part of the JavaScript files already loaded into the browser. To avoid this limitation, we need to add an instruction to load JavaScript code dynamically. This can be accomplished by using JSAN library, which allows loading a script from a known location.

We can now write code like this:
 

<!--[if !vml]-->



<!--[endif]-->

The import directive implies that specified file needs to be loaded, and so the DynamicValue now can be defined in the DynamicScript.js file. Here is the code that does the import:

 

<!--[if !vml]-->



<!--[endif]-->
Making it flexible

We showed above the basic mechanics of how to load JavaScriptBeans from an XML file. We can now think of other things that would be useful to add to this framework.
 

First, we'd like to have an ability to handle arrays. We can do this in several ways. First, we could require that objects pre-declare all array properties in the constructor like this: myArray = [].

So when the new object is instantiated, the property would set. Then when we are add bean property, we can check and see if it is array and handle it appropriately. Another idea is to specify that a property represents arrays in XML file. This can be accomplished with array=”true” property.

 

Other things that you might want to add include the handling of maps, ability to define variables and referencing them so that you do not need to create more objects than necessary.

 
Conclusion
 
Instantiate JavaScript objects from XML file comes particularly handy when you are developing Mozilla extensions with dynamic behavior. But even if your are doing pure web site applications, using this technique can greatly simplify your configuration and setup process. Beans and naming conventions are good patterns to have under your belt. They were handy in Java and they are still handy in JavaScript.


Don't miss Alex Iskold's blog: http://alexiskold.wordpress.com
About Alex Iskold
Alex Iskold is the Founder and CEO of adaptiveblue (http://www.adaptiveblue.com), where he is developing browser personalization technology. His previous startup, Information Laboratory, created innovative software analysis and visualization tool called Small Worlds. After Information Laboratory was acquired by IBM, Alex worked as the architect of IBM Rational Software Analysis tools. Before starting adaptiveblue, Alex was the Chief Architect at DataSynapse, where he developed GridServer and FabricServer virtualization platforms. He holds M.S. in Computer Science from New York University, where he taught an award-winning software engineering class for undergraduate students. He can be reached at alex.iskold@gmail.com.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Six month ago, Alex Iskold switched from J2EE Grid Computing to Web 2.0, JavaScript and Firefox extension development. He has been writing in Web 2.0 Journal about his experiences - see 'From J2EE to JavaScript.' This is the next instalment...


Your Feedback
AJAXWorld News Desk wrote: Six month ago, Alex Iskold switched from J2EE Grid Computing to Web 2.0, JavaScript and Firefox extension development. He has been writing in Web 2.0 Journal about his experiences - see 'From J2EE to JavaScript.' This is the next instalment...
Enterprise Open Source Magazine Latest Stories . . .
Oracle seems to have divided the open source ranks over the MySQL delay it’s having closing its acquisition of Sun. Eben Moglin, the GPL’s most ardent defender and delineator, the lawyer who has worked hand in glove for years with the Free Software Foundation’s founder Richard Stallman...
Cloud computing is a game changer. The cloud is disrupting traditional software and hardware business models by disrupting how IT service gets delivered. Entrepreneurial opportunities abound as this classic disruptive technology begins to proliferate, so it is no surprise that SYS-CON'...
The irony is that Oracle has advanced MySQL, lost money in the process, and helped its competitors - all at the same time. When Oracle buys Sun and controls MySQL the gift (other than to Microsoft SQL Server) keeps on giving as the existential threat to RDBs is managed by Redwood Shore...
WSO2, the open source SOA company, today announced the launch of the WSO2 Cloud Platform. Available today, the new WSO2 Cloud Platform features a family of WSO2 Cloud Virtual Machines; WSO2 Cloud Connectors for enabling fast, secure cloud services; and the multi-tenant WSO2 Governance-...
Now, the open source Mozilla Thunderbird client software can be used with Open-Xchange collaboration software. The "Community OXtender for Thunderbird" software connector gives users full access to appointments and contacts stored in the Open-Xchange Server and enables them to use Thun...
Morph Labs, a leading provider of enterprise cloud computing technology, today announced an introductory trial of the Morph CloudServer, an open, standards-based server IT organizations can use to rapidly model and evaluate their cloud implementations. A miniature "Cloud Environment in...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON Featured Whitepapers
ADS BY GOOGLE