The Application: PushToTest TestMaker
As part of our work on the PushToTest TestMaker open source project, we ran into numerous issues and resolutions that we felt should be shared.
Some background on what we used:
- Appcelerator Titanium Developer
- Javascript
- jQuery 1.3.2
- jQuery template plugin
- jQuery scrollTo plugin
- ExtJS 3.0 rc 3
First Issue To Address
One of the key first issues we ran into was how to render the html using jQuery on the client side for the Titanium application without doing too much manual DOM manipulation. Also, since most of the information being rendered would be in arrays, it would preferably make displaying arrays easier.
The Solution
Our solution came in the form of the jQuery template plugin.
For our implementation, we created html code snippets that we placed in individual html files. Each html file would be loaded and be passed into the $.template method which would then be passed into a .append method along with an object whose values would be injected into the html.
The reason why templating and more specifically why breaking the html into snippets was necessary is almost entirely because we wanted to be able to iterate through multiple objects and display them the cleanest and most efficient way possible.
Example with the PushToTest Datasource
For an example I’ve lain out how we rendered the information for datasources. Datasources are objects that have names, types, and resource names.
First we created a datasources.hmtl page that will be a container for all the datasources rendered:
<div class="panelmessage">
Make this a data-driven test.
</div>
<div class="form">
<div class="title">
Data Production Libraries (DPLs) provide operational test data to the tests as they operate. For example, a DPL reads data from a relational database, comma-separated-value (CSV) file, or data generating object and injects it into a test script. <a id="ShowTutorials" name="ShowTutorials" class="link">Read more about it.</a>
</div>
<div id="datasources_iterator" style="display:none" class="">
</div>
<div class="adddatasource">
<a id="adddatasource" name="adddatasource" class="link" dirty="true">Add DPL</a>
</div>
</div>
Then, we created a _datasourcestemplate.html file with all the attributes for a datasource specified:
<div id="datasources_iterator${id}" class="datasource_container">
<input type="hidden" name="id" value="${id}" fieldset="datasource"/>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="name" value="${name}" fieldset="datasource" class="name"/></td>
<td><input type="image" name="delete" value="${id}" src="images/cross.png" fieldset="datasource"/></td>
</tr>
<tr>
<td>Type</td>
<td>
<select id="datatypeselect${id}" name="datatypeselect" fieldset="datasource">
<option value="HashDPL">HashDPL</option>
<option value="CSVDPL">CSVDPL</option>
<option value="RDBMSDPL">RDBMSDPL</option>
<option value="TheLocker">TheLocker</option>
</select>
</td>
</tr>
<tr>
<td>Resource Name</td>
<td><select id="rscname${id}" datasourceid="${id}" name="rscname" fieldset="datasource"></select> (From Resources)</td>
</tr>
</table>
</div>
Then in the javascript we use this code to get that content and template it:
$.get('content/_datasourcestemplate.html', function(html) {
Editor.datasourcesTemplate = $.template(html);
});
for (var i=0;i<Editor.currentopenfile.datasources.length;i++) {
var datasource = Editor.currentopenfile.datasources[i];
$('#datasources_iterator').append(Editor.datasourcesTemplate, datasource);
}
Using this method we were able to iterate through the datasource objects and inject them into the template, then append it to the datasources.html page.
Post implementation lessons
As you can see in the _datasourcestemplate.html code, we placed the datasource object’s id all over the place. Originally this was needed because of the way we were using jQuery to get values from the DOM. After doing some more research we found out better ways to gain access to the DOM which is what I will cover in the next post.
Technorati Tags: DATA series, jQuery, PushToTest, templating, titanium