The DataView control: going imperative, take II
A few posts back, we started talking about how we can use the DataView control in an imperative way. At the time, we were still working with preview 5. Now that preview 6 is out, we’ll keep discussing this topic, but we’ll be upgrading our code from preview5 to preview6.
<head> <title></title> <script src="Scripts/MicrosoftAjax/start.debug.js"
- We’re relying on the script loader object’s ability to load scripts on demand. Notice that we only add the start script and then require the extra downloads by using the Sys.require method helper.
- we’re already using preview 6 and that means more helpers! The script loader object ends up adding several helper methods to the Sys.create object which can be used for creating components and behaviors previously introduced through a defineScript(s) method call. And that’s why we can create the DataView control by using the Sys.create.dataview method! Notice that this method expects only two parameters: a reference to the DOM element to which the AJAX control will be associated and a list of properties/events which will be initialized with the indicated values;
- as I’ve said, the itemRendered event is fired after each item obtained from the data property is “rendered” into HTML through a template instantiation. The function which handles the event receives two parameters: a reference to the control that generated the event and a reference to the Sys.UI.TemplateContext obtained through the template instantiation (recall from our template parsing study that template instantiation ends up returning an object of this type). Sys.UI.TemplateContext objects are interesting and we’ll return to them in the next post. For now, you can see that they let us get the object that was used for instantiating the template (notice the dataItem property) and get the HTML nodes that resulted from the template instantiation (nodes property). In our previous case, template instantiation ends up producing a single node (LI) and we reuse that knowledge for setting its content.
And there you go: as you can see, you’re not forced to “pollute” your markup with templates since you can do everything from script. However, I believe that “HTML pollution” is a small price to pay here, especially when you’ve got nested templates (more on future posts): the simplicity and productivity boost you get from them is undeniable!
And that’s it for now. Stay tuned for more on MS AJAX.