| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 | <title>Step 6 of test for Controllers, uses a ListController.</title> |
| 6 | <style type="text/css"> |
| 7 | @import "../css/app-format.css"; |
| 8 | @import "../../../../dijit/themes/claro/claro.css"; |
| 9 | </style> |
| 10 | <script type="text/javascript" data-dojo-config="parseOnLoad:0,isDebug:1,async:1,mvc:{debugBindings:1}" src="../../../../dojo/dojo.js"></script> |
| 11 | <script type="text/javascript"> |
| 12 | var ctrl; |
| 13 | |
| 14 | require([ |
| 15 | 'dojo/parser', |
| 16 | 'dojo/_base/declare', |
| 17 | 'dojo/ready', |
| 18 | 'dojox/mvc/getStateful', |
| 19 | 'dojox/mvc/ListController', |
| 20 | 'dojox/mvc/parserExtension', |
| 21 | 'dijit/form/TextBox', |
| 22 | 'dijit/form/Button', |
| 23 | 'dijit/form/RadioButton', |
| 24 | 'dojox/mvc/Group', |
| 25 | 'dojox/mvc/WidgetList', |
| 26 | 'dojox/mvc/_InlineTemplateMixin', |
| 27 | 'dojox/mvc/Output' |
| 28 | ], function(parser, declare, ready, getStateful, ListController){ |
| 29 | |
| 30 | // Initial data |
| 31 | var listData = [ |
| 32 | { |
| 33 | "id" : "360324", |
| 34 | "AddressName" : "Home", |
| 35 | "Checked" : true, |
| 36 | "FullName" : "Mr. John Doe", |
| 37 | "Email" : "[email protected]", |
| 38 | "AddressLine1" : "123 Valley Rd.", |
| 39 | "AddressLine2" : "", |
| 40 | "City" : "Cary", |
| 41 | "State" : "NC", |
| 42 | "Zip" : "27513" |
| 43 | }, |
| 44 | { |
| 45 | "id" : "360325", |
| 46 | "AddressName" : "Work", |
| 47 | "Checked" : false, |
| 48 | "FullName" : "Mr. John Doe", |
| 49 | "Email" : "[email protected]", |
| 50 | "AddressLine1" : "4567 S Miami Blvd", |
| 51 | "AddressLine2" : "Bldg:502, Office:M120", |
| 52 | "City" : "Durham", |
| 53 | "State" : "NC", |
| 54 | "Zip" : "27703" |
| 55 | } |
| 56 | ]; |
| 57 | |
| 58 | // To move from ModelRefController to also use ListController, I had to add the following |
| 59 | // Need to require declare, and ListController, I also needed WidgetList and _InlineTemplateMixin. |
| 60 | // mvc/parserExtension is still needed to bind to class with data-mvc-bindings for 'AddressLine2' |
| 61 | // Decided to setup a ctrlclz with ListController to have the functions as part of the controller. |
| 62 | // The data had to be changed to be array data. |
| 63 | // The html was updated to use a WidgetList to show a radio button group with the choices, and to use a |
| 64 | // "cursor" binding for the selected address. |
| 65 | // Added a transform (transformRadioChecked) to change the cursorIndex when a different AddressName radio button is |
| 66 | // selected. Also added an addEmpty function to add a New Address to the list. |
| 67 | // |
| 68 | |
| 69 | var ctrlclz = declare([ListController], { |
| 70 | |
| 71 | transformRadioChecked : { |
| 72 | format: function(checked){ |
| 73 | if(checked){ |
| 74 | ctrl.set('cursorIndex', this.target.index); |
| 75 | console.log("in transformRadioChecked setting cursorIndex to "+this.target.index); |
| 76 | } |
| 77 | return checked; |
| 78 | }, |
| 79 | parse: function(checked){ |
| 80 | return checked; |
| 81 | } |
| 82 | }, |
| 83 | |
| 84 | transformAddress2Class : { |
| 85 | format: function(value){ |
| 86 | if(value && value.trim().length > 0){ |
| 87 | return "row" // if AddressLine2 is set return row for class |
| 88 | } |
| 89 | return "hiderow"; // if AddressLine2 is not set return hiderow for class |
| 90 | } |
| 91 | }, |
| 92 | |
| 93 | addEmpty : function(empty){ |
| 94 | var idx = this.model.length; |
| 95 | var data = { |
| 96 | "id" : Math.random(), |
| 97 | "AddressName" : "New Address", |
| 98 | "Checked" : true, |
| 99 | "FullName" : "", |
| 100 | "Email" : "", |
| 101 | "AddressLine1" : "", |
| 102 | "AddressLine2" : "", |
| 103 | "City" : "", |
| 104 | "State" : "", |
| 105 | "Zip" : "" |
| 106 | }; |
| 107 | if(!empty){ |
| 108 | data = { |
| 109 | "id" : Math.random(), |
| 110 | "AddressName" : "Copy of "+this.cursor.AddressName, |
| 111 | "Checked" : true, |
| 112 | "FullName" : "" || this.cursor.Name, |
| 113 | "Email" : this.cursor.Email, |
| 114 | "AddressLine1" : this.cursor.AddressLine1, |
| 115 | "AddressLine2" : this.cursor.AddressLine2, |
| 116 | "City" : this.cursor.City, |
| 117 | "State" : this.cursor.State, |
| 118 | "Zip" : this.cursor.Zip |
| 119 | }; |
| 120 | } |
| 121 | this.model.splice(idx, 0, getStateful(data)); |
| 122 | } |
| 123 | |
| 124 | }); |
| 125 | ctrl = new ctrlclz({model: getStateful(listData)}); |
| 126 | |
| 127 | parser.parse(); |
| 128 | ctrl.model[0].set("Checked", true); // called to init the radio button selection. |
| 129 | }); |
| 130 | </script> |
| 131 | </head> |
| 132 | <body class="claro"> |
| 133 | <script type="dojo/require">at: "dojox/mvc/at"</script> |
| 134 | <div id="wrapper"> |
| 135 | <div id="main"> |
| 136 | <div> |
| 137 | <h2>Step 6 of test for Controllers, uses a ListController.</h2> |
| 138 | <h2>Select the Shipping Address to use.</h2> |
| 139 | </div> |
| 140 | |
| 141 | <div data-dojo-type="dojox/mvc/WidgetList" |
| 142 | data-dojo-mixins="dojox/mvc/_InlineTemplateMixin" |
| 143 | data-dojo-props="children: at(ctrl, 'model'), partialRebuild:true"> |
| 144 | <script type="dojox/mvc/InlineTemplate"> |
| 145 | <div> |
| 146 | <input type="radio" data-dojo-type="dijit/form/RadioButton" name="AddressName" id="radio${indexAtStartup}" |
| 147 | data-dojo-props="index: ${indexAtStartup}, |
| 148 | // value: at('rel:', 'AddressName'), // do not set value on the radio button or it will also set checked. |
| 149 | checked: at('rel:', 'Checked').transform(ctrl.transformRadioChecked)"/> |
| 150 | <label class="radioLabel" for="radio${indexAtStartup}" data-dojo-type="dojox/mvc/Output" |
| 151 | data-dojo-props="value: at('rel:', 'AddressName')"></label> |
| 152 | <br /> |
| 153 | </div> |
| 154 | </script> |
| 155 | </div> |
| 156 | <div class="fullrow"> |
| 157 | <button class="buttoncell" id="add" type="button" data-dojo-type="dijit/form/Button" |
| 158 | data-dojo-props="onClick: function(){ ctrl.addEmpty(true); }">New Address</button> |
| 159 | <button class="buttoncell" id="addcopy" type="button" data-dojo-type="dijit/form/Button" |
| 160 | data-dojo-props="onClick: function(){ ctrl.addEmpty(false); }">New copy of selected.</button> |
| 161 | </div> |
| 162 | |
| 163 | <div class="spacer"></div> |
| 164 | <h2>Enter the Shipping Address</h2> |
| 165 | |
| 166 | <div class="fullrow" data-dojo-type="dojox/mvc/Group" data-dojo-props="target: at(ctrl,'cursor')"> |
| 167 | <div class="fullrow"> |
| 168 | <label class="cell" for="AddressNameInput">Address Name:</label> |
| 169 | <input class="cell" id="AddressNameInput" data-dojo-type="dijit/form/TextBox" |
| 170 | data-dojo-props="value: at('rel:', 'AddressName')"> |
| 171 | </div> |
| 172 | <br/> |
| 173 | <div class="fullrow"> |
| 174 | <label class="cell" for="nameInput">Full Name:</label> |
| 175 | <input class="cell" id="nameInput" data-dojo-type="dijit/form/TextBox" |
| 176 | data-dojo-props="value: at('rel:', 'FullName')"> |
| 177 | </div> |
| 178 | <div class="fullrow"> |
| 179 | <label class="cell" for="AddressLine1Input">Address Line1:</label> |
| 180 | <input class="cell" id="AddressLine1Input" data-dojo-type="dijit/form/TextBox" |
| 181 | data-dojo-props="value: at('rel:', 'AddressLine1')"> |
| 182 | </div> |
| 183 | <div class="fullrow"> |
| 184 | <label class="cell" for="AddressLine2Input">Address Line2:</label> |
| 185 | <input class="cell" id="AddressLine2Input" data-dojo-type="dijit/form/TextBox" |
| 186 | data-dojo-props="value: at('rel:', 'AddressLine2')"> |
| 187 | </div> |
| 188 | <div class="fullrow"> |
| 189 | <label class="cell" for="cityInput">City:</label> |
| 190 | <input class="cell" id="cityInput" data-dojo-type="dijit/form/TextBox" |
| 191 | data-dojo-props="value: at('rel:', 'City')"> |
| 192 | </div> |
| 193 | <div class="fullrow"> |
| 194 | <label class="cell" for="stateInput">State:</label> |
| 195 | <input class="cell" id="stateInput" data-dojo-type="dijit/form/TextBox" |
| 196 | data-dojo-props="value: at('rel:', 'State')"> |
| 197 | </div> |
| 198 | <div class="fullrow"> |
| 199 | <label class="cell" for="zipInput">Zipcode:</label> |
| 200 | <input class="cell" id="zipInput" data-dojo-type="dijit/form/TextBox" |
| 201 | data-dojo-props="value: at('rel:', 'Zip')"> |
| 202 | </div> |
| 203 | </div> |
| 204 | <br/> |
| 205 | <div class="fullrow" data-dojo-type="dojox/mvc/Group" data-dojo-props="target: at(ctrl,'cursor')"> |
| 206 | <div> |
| 207 | <h2>Verify the shipping address for: |
| 208 | <div class="namecell" id="AddressNameOutput" data-dojo-type="dojox/mvc/Output" |
| 209 | data-dojo-props="value: at('rel:', 'AddressName')"></div> |
| 210 | </h2> |
| 211 | </div> |
| 212 | <div class="fullrow"> |
| 213 | <div class="boldnamecell" data-dojo-type="dojox/mvc/Output" |
| 214 | data-dojo-props="value: at('rel:', 'FullName')"></div> |
| 215 | </div> |
| 216 | <div class="fullrow"> |
| 217 | <div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 218 | data-dojo-props="value: at('rel:', 'AddressLine1')"></div> |
| 219 | </div> |
| 220 | <div class="hiderow" data-mvc-bindings="class: at('rel:', 'AddressLine2').direction(at.from).transform(ctrl.transformAddress2Class)"> |
| 221 | <div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 222 | data-dojo-props="value: at('rel:', 'AddressLine2')"></div> |
| 223 | </div> |
| 224 | <div class="fullrow"> |
| 225 | <div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 226 | data-dojo-props="exprchar:'%', value: at('rel:', 'City')">%{this.value},</div> |
| 227 | <div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 228 | data-dojo-props="value: at('rel:', 'State')"></div> |
| 229 | <div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 230 | data-dojo-props="value: at('rel:', 'Zip')"></div> |
| 231 | </div> |
| 232 | </div> |
| 233 | </div> |
| 234 | </div> |
| 235 | </body> |
| 236 | </html> |