MSDN Magazine - June 2008 - (Page 32) } } builder = new Samples.MarkupBuilder(); builder.loadHeader($get("header")); builder.loadFooter($get("footer")); builder.loadItemTemplate($get("item")); You can embed HTML templates directly in the page using invisible DIVs. However, this only works if the markup in the block is well formed. A better option would be to embed templates as XML data islands, as shown here: #Symbol #Quote #Change As you can see, the template is a chunk of HTML that refers to binding fields using a custom notation. In this case, I’m using the #PropertyName expression to indicate the placeholder for a bound value. When data is available, you just invoke the bind method on the markup builder: function _getLiveQuotes() { Samples.WebServices.LiveQuoteService.Update(onDataAvailable); } function onDataAvailable(results) { var temp = builder.bind(results); $get("grid").innerHTML = temp; } Figure 7 New Page Using the Markup Builder Needless to say, the element named grid is the placeholder for the final output in this example. The MarkupBuilder class is based on the Microsoft AJAX Client Library. Its full source code is found in Figure 6. Internally, the markup builder uses a Sys.StringBuilder object to compose the HTML string. It first adds the header template (if any) and then proceeds to loop over the bound data. Finally, it adds the footer template. In this implementation, header and footer templates are not data-bound. (Figure 7 shows a page using the markup builder.) Is there any difference between specifying the template as a plain string or as an embedded DOM subtree or XML data island? From the markup builder’s perspective, it doesn’t really matter—all that it needs is a string to append to the string builder buffer. From the developer’s perspective, using DOM templates may be a better option because you can more easily validate your template and might also be able to download the template from a URL. Designed to work only with collections of data, the solution presented here follows the guidelines of other free frameworks for client-side templating. One of these frameworks that I would like to specifically mention is PrototypeJS. For more information, samples, and source code, refer to prototypejs.org/api/template. Is there anything that you can improve? Absolutely! Note that the code presented in this column implements the minimum set of features one would expect from a browser-side template and binding engine. As you can see in Figure 7, the current implementation does not have colored type to distinguish the prices of rising and fall32 msdn magazine ing stocks, as Figure 2 had. In the code of Figure 2, I handle the RowDataBound event of the GridView control and directly modify the style of involved cells: void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.Cells[2].Text.StartsWith("+")) e.Row.Cells[2].ForeColor = Color.Green; if (e.Row.Cells[2].Text.StartsWith("-")) e.Row.Cells[2].ForeColor = Color.Red; } Within a partial rendering solution, this styling happens on the server and therefore has the ability to leverage the rich program- Further Considerations The goal of the BST pattern is to separate the code that produces a view of the data from the data itself. ming model of server data-bound controls. In a client-side solution, you need a more sophisticated markup builder that accepts a function delegate that styles any piece of bound data before it is appended to the buffer. Another more important difference exists between the code that produced Figures 2 and 7 that doesn’t show up through the printed figures. To discover this difference, try running both pages and click on the other buttons you see (Weather Forecasts and Start Some Task). The page depicted in Figure 7 does not stop Cutting Edge http://www.prototypejs.org/api/template
Table of Contents Feed for the Digital Edition of MSDN Magazine - June 2008 MSDN Magazine - June 2008 Contents Toolbox CLR Inside Out Cutting Edge Patterns In Practice SAAS Concurrency Robotics Form Filler GUI Library Service Station Foundations Windows With C++ Concurrent Affairs Going Places { End Bracket } MSDN Magazine - June 2008 MSDN Magazine - June 2008 - Contents (Page Cover1) MSDN Magazine - June 2008 - Contents (Page Cover2) MSDN Magazine - June 2008 - Contents (Page 1) MSDN Magazine - June 2008 - Contents (Page 2) MSDN Magazine - June 2008 - Contents (Page 3) MSDN Magazine - June 2008 - Contents (Page 4) MSDN Magazine - June 2008 - Contents (Page 5) MSDN Magazine - June 2008 - Contents (Page 6) MSDN Magazine - June 2008 - Contents (Page 7) MSDN Magazine - June 2008 - Contents (Page 8) MSDN Magazine - June 2008 - Contents (Page 9) MSDN Magazine - June 2008 - Contents (Page 10) MSDN Magazine - June 2008 - Toolbox (Page 11) MSDN Magazine - June 2008 - Toolbox (Page 12) MSDN Magazine - June 2008 - Toolbox (Page 13) MSDN Magazine - June 2008 - Toolbox (Page 14) MSDN Magazine - June 2008 - CLR Inside Out (Page 15) MSDN Magazine - June 2008 - CLR Inside Out (Page 16) MSDN Magazine - June 2008 - CLR Inside Out (Page 17) MSDN Magazine - June 2008 - CLR Inside Out (Page 18) MSDN Magazine - June 2008 - CLR Inside Out (Page 19) MSDN Magazine - June 2008 - CLR Inside Out (Page 20) MSDN Magazine - June 2008 - CLR Inside Out (Page 21) MSDN Magazine - June 2008 - CLR Inside Out (Page 22) MSDN Magazine - June 2008 - CLR Inside Out (Page 23) MSDN Magazine - June 2008 - CLR Inside Out (Page 24) MSDN Magazine - June 2008 - Cutting Edge (Page 25) MSDN Magazine - June 2008 - Cutting Edge (Page 26) MSDN Magazine - June 2008 - Cutting Edge (Page 27) MSDN Magazine - June 2008 - Cutting Edge (Page 28) MSDN Magazine - June 2008 - Cutting Edge (Page 29) MSDN Magazine - June 2008 - Cutting Edge (Page 30) MSDN Magazine - June 2008 - Cutting Edge (Page 31) MSDN Magazine - June 2008 - Cutting Edge (Page 32) MSDN Magazine - June 2008 - Cutting Edge (Page 33) MSDN Magazine - June 2008 - Cutting Edge (Page 34) MSDN Magazine - June 2008 - Cutting Edge (Page 35) MSDN Magazine - June 2008 - Cutting Edge (Page 36) MSDN Magazine - June 2008 - Patterns In Practice (Page 37) MSDN Magazine - June 2008 - Patterns In Practice (Page 38) MSDN Magazine - June 2008 - Patterns In Practice (Page 39) MSDN Magazine - June 2008 - Patterns In Practice (Page 40) MSDN Magazine - June 2008 - Patterns In Practice (Page 41) MSDN Magazine - June 2008 - Patterns In Practice (Page 42) MSDN Magazine - June 2008 - Patterns In Practice (Page 43) MSDN Magazine - June 2008 - SAAS (Page 44) MSDN Magazine - June 2008 - SAAS (Page 45) MSDN Magazine - June 2008 - SAAS (Page 46) MSDN Magazine - June 2008 - SAAS (Page 47) MSDN Magazine - June 2008 - SAAS (Page 48) MSDN Magazine - June 2008 - SAAS (Page 49) MSDN Magazine - June 2008 - SAAS (Page 50) MSDN Magazine - June 2008 - SAAS (Page 51) MSDN Magazine - June 2008 - SAAS (Page 52) MSDN Magazine - June 2008 - SAAS (Page 53) MSDN Magazine - June 2008 - Concurrency (Page 54) MSDN Magazine - June 2008 - Concurrency (Page 55) MSDN Magazine - June 2008 - Concurrency (Page 56) MSDN Magazine - June 2008 - Concurrency (Page 57) MSDN Magazine - June 2008 - Concurrency (Page 58) MSDN Magazine - June 2008 - Concurrency (Page 59) MSDN Magazine - June 2008 - Concurrency (Page 60) MSDN Magazine - June 2008 - Concurrency (Page 61) MSDN Magazine - June 2008 - Concurrency (Page 62) MSDN Magazine - June 2008 - Concurrency (Page 63) MSDN Magazine - June 2008 - Robotics (Page 64) MSDN Magazine - June 2008 - Robotics (Page 65) MSDN Magazine - June 2008 - Robotics (Page 66) MSDN Magazine - June 2008 - Robotics (Page 67) MSDN Magazine - June 2008 - Robotics (Page 68) MSDN Magazine - June 2008 - Robotics (Page 69) MSDN Magazine - June 2008 - Robotics (Page 70) MSDN Magazine - June 2008 - Robotics (Page 71) MSDN Magazine - June 2008 - Robotics (Page 72) MSDN Magazine - June 2008 - Robotics (Page 73) MSDN Magazine - June 2008 - Robotics (Page 74) MSDN Magazine - June 2008 - Robotics (Page 75) MSDN Magazine - June 2008 - Robotics (Page 76) MSDN Magazine - June 2008 - Robotics (Page 77) MSDN Magazine - June 2008 - Robotics (Page 78) MSDN Magazine - June 2008 - Form Filler (Page 79) MSDN Magazine - June 2008 - Form Filler (Page 80) MSDN Magazine - June 2008 - Form Filler (Page 81) MSDN Magazine - June 2008 - Form Filler (Page 82) MSDN Magazine - June 2008 - Form Filler (Page 83) MSDN Magazine - June 2008 - Form Filler (Page 84) MSDN Magazine - June 2008 - Form Filler (Page 85) MSDN Magazine - June 2008 - GUI Library (Page 86) MSDN Magazine - June 2008 - GUI Library (Page 87) MSDN Magazine - June 2008 - GUI Library (Page 88) MSDN Magazine - June 2008 - GUI Library (Page 89) MSDN Magazine - June 2008 - GUI Library (Page 90) MSDN Magazine - June 2008 - GUI Library (Page 91) MSDN Magazine - June 2008 - GUI Library (Page 92) MSDN Magazine - June 2008 - GUI Library (Page 93) MSDN Magazine - June 2008 - GUI Library (Page 94) MSDN Magazine - June 2008 - GUI Library (Page 95) MSDN Magazine - June 2008 - GUI Library (Page 96) MSDN Magazine - June 2008 - Service Station (Page 97) MSDN Magazine - June 2008 - Service Station (Page 98) MSDN Magazine - June 2008 - Service Station (Page 99) MSDN Magazine - June 2008 - Service Station (Page 100) MSDN Magazine - June 2008 - Service Station (Page 101) MSDN Magazine - June 2008 - Service Station (Page 102) MSDN Magazine - June 2008 - Service Station (Page 103) MSDN Magazine - June 2008 - Service Station (Page 104) MSDN Magazine - June 2008 - Foundations (Page 105) MSDN Magazine - June 2008 - Foundations (Page 106) MSDN Magazine - June 2008 - Foundations (Page 107) MSDN Magazine - June 2008 - Foundations (Page 108) MSDN Magazine - June 2008 - Foundations (Page 109) MSDN Magazine - June 2008 - Foundations (Page 110) MSDN Magazine - June 2008 - Foundations (Page 111) MSDN Magazine - June 2008 - Foundations (Page 112) MSDN Magazine - June 2008 - Windows With C++ (Page 113) MSDN Magazine - June 2008 - Windows With C++ (Page 114) MSDN Magazine - June 2008 - Windows With C++ (Page 115) MSDN Magazine - June 2008 - Windows With C++ (Page 116) MSDN Magazine - June 2008 - Windows With C++ (Page 117) MSDN Magazine - June 2008 - Windows With C++ (Page 118) MSDN Magazine - June 2008 - Concurrent Affairs (Page 119) MSDN Magazine - June 2008 - Concurrent Affairs (Page 120) MSDN Magazine - June 2008 - Concurrent Affairs (Page 121) MSDN Magazine - June 2008 - Concurrent Affairs (Page 122) MSDN Magazine - June 2008 - Concurrent Affairs (Page 123) MSDN Magazine - June 2008 - Concurrent Affairs (Page 124) MSDN Magazine - June 2008 - Concurrent Affairs (Page 125) MSDN Magazine - June 2008 - Concurrent Affairs (Page 126) MSDN Magazine - June 2008 - Going Places (Page 127) MSDN Magazine - June 2008 - Going Places (Page 128) MSDN Magazine - June 2008 - Going Places (Page 129) MSDN Magazine - June 2008 - Going Places (Page 130) MSDN Magazine - June 2008 - Going Places (Page 131) MSDN Magazine - June 2008 - { End Bracket } (Page 132) MSDN Magazine - June 2008 - { End Bracket } (Page Cover3) MSDN Magazine - June 2008 - { End Bracket } (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.