The Sys.create object helper

This is becoming quite a long series, but what can I do? There’s simply lots of things to talk about in the new MS AJAX preview 6. As you probably recall (if you’ve been following along since I’ve started writing compulsorily about MS AJAX), we started with code written against preview 5 and then moved to preview 6 when it was released.

One of the new helpers I’ve been using is the new Sys.create object. This object contains several shortcuts for creating components. For instance, in the previous post, I’ve used it to create a new DataView component (instead of relying in the old $create helper).

Sys.create object’s properties are generated dynamically during initialization time. The new script loader object is responsible for populating the object when it parses the info sent to a defineScript(s) method call. In practice, all the components that are specified through the components property of the script info object end up:

  • populating the Sys.components object with information about a specific component;
  • populating the Sys.create object with a new property (named after the script info object’s name property) which references a method created on the fly (this method works like a proxy and is responsible for instantiating the component).

At this time, it might be really interesting to see the code that is generated for one of this helper methods. Lets take a look at what Sys.create.dataView ends up calling:

function anonymous(target, properties) {
        this, "dataView", Sys._create, 
arguments, false,this); }

Btw,I’ve removed the auto-generated comments which do really help with the parameters expected by each function. The Sys.loader._callPlugin is a private method which ends up calling other private methods that are responsible for doing all the work (notice that they need to create a new component, set the indicated properties and register it with the Sys.Application object).

Using the Sys.create object’s method helpers lead to less complexity since they require only two parameters:

  • a reference to the DOM element which will be attached to the component;
  • a JavaScript literal object with the properties and events you want to set.

One of the main differences when you compare this approach with the use of the old $create method is that you no longer separate properties from events: with the Sys.create helpers, everything is mixed up in a single literal object. In practice, the helpers start by checking for an event (ie, then start by trying to find a add_xxx method, where xxx is the name of the property you’ve specified in the literal object) before checking for a property on the component (don’t forget that property means finding a set_xxx method, where xxx is the name of the property).

This shouldn’t really bring any problems because it really is bad practice to give the same name to events and properties. Before ending, there’s still time to mention that MS AJAX preview 6 introduces some helpers: adoNetDataContext, adoNetServiceProxy, dataContext and dataView. And I guess that’s it. Stay tuned for more.


~ by Luis Abreu on October 22, 2009.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: