| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 |
<title>Step 7 of test for Controllers, uses an EditControllerMixin and ListControllerMixin.</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/EditModelRefController', |
| 20 |
'dojox/mvc/ListController', |
| 21 |
'dojox/mvc/parserExtension', |
| 22 |
'dijit/form/TextBox', |
| 23 |
'dijit/form/Button', |
| 24 |
'dijit/form/RadioButton', |
| 25 |
'dojox/mvc/Group', |
| 26 |
'dojox/mvc/WidgetList', |
| 27 |
'dojox/mvc/_InlineTemplateMixin', |
| 28 |
'dojox/mvc/Output' |
| 29 |
], function(parser, declare, ready, getStateful, EditModelRefController, ListController){ |
| 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 EditModelRefController and a ListController, |
| 59 |
// Need to require declare, and EditModelRefController, ListController, |
| 60 |
// I also needed WidgetList and _InlineTemplateMixin. |
| 61 |
// |
| 62 |
// Needed to setup ctrlclz with EditModelRefController and ListController, and the data needs to be array data and the |
| 63 |
// call to new ctrlclz uses sourceModel instead of model, and sets holdModelUntilCommit |
| 64 |
// The html was updated to use a WidgetList to show a radio button group with the choices, and to use a |
| 65 |
// "cursor" binding for the selected address. |
| 66 |
// Added a transform (transformRadioChecked) to change the cursorIndex when a different AddressName radio button is |
| 67 |
// selected. Also added an addEmpty function to add a New Address to the list. |
| 68 |
// |
| 69 |
|
| 70 |
var ctrlclz = declare([EditModelRefController, ListController], { |
| 71 |
|
| 72 |
transformRadioChecked : { |
| 73 |
format: function(checked){ |
| 74 |
if(checked){ |
| 75 |
ctrl.set('cursorIndex', this.target.index); |
| 76 |
console.log("in transformRadioChecked setting cursorIndex to "+this.target.index); |
| 77 |
} |
| 78 |
return checked; |
| 79 |
}, |
| 80 |
parse: function(checked){ |
| 81 |
return checked; |
| 82 |
} |
| 83 |
}, |
| 84 |
|
| 85 |
transformAddress2Class : { |
| 86 |
format: function(value){ |
| 87 |
if(value && value.trim().length > 0){ |
| 88 |
return "row" // if AddressLine2 is set return row for class |
| 89 |
} |
| 90 |
return "hiderow"; // if AddressLine2 is not set return hiderow for class |
| 91 |
} |
| 92 |
}, |
| 93 |
|
| 94 |
addEmpty : function(empty){ |
| 95 |
var idx = this.model.length; |
| 96 |
var data = { |
| 97 |
"id" : Math.random(), |
| 98 |
"AddressName" : "New Address", |
| 99 |
"Checked" : true, |
| 100 |
"FullName" : "", |
| 101 |
"Email" : "", |
| 102 |
"AddressLine1" : "", |
| 103 |
"AddressLine2" : "", |
| 104 |
"City" : "", |
| 105 |
"State" : "", |
| 106 |
"Zip" : "" |
| 107 |
}; |
| 108 |
if(!empty){ |
| 109 |
data = { |
| 110 |
"id" : Math.random(), |
| 111 |
"AddressName" : "Copy of "+this.cursor.AddressName, |
| 112 |
"Checked" : true, |
| 113 |
"FullName" : "" || this.cursor.Name, |
| 114 |
"Email" : this.cursor.Email, |
| 115 |
"AddressLine1" : this.cursor.AddressLine1, |
| 116 |
"AddressLine2" : this.cursor.AddressLine2, |
| 117 |
"City" : this.cursor.City, |
| 118 |
"State" : this.cursor.State, |
| 119 |
"Zip" : this.cursor.Zip |
| 120 |
}; |
| 121 |
} |
| 122 |
this.model.splice(idx, 0, getStateful(data)); |
| 123 |
} |
| 124 |
|
| 125 |
}); |
| 126 |
ctrl = new ctrlclz({sourceModel: getStateful(listData), holdModelUntilCommit: false}); |
| 127 |
|
| 128 |
parser.parse(); |
| 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 7 of test for Controllers, uses an EditModelRefController and 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 |
|
| 206 |
<div class="fullrow"> |
| 207 |
<button class="buttoncell" id="reset" type="button" data-dojo-type="dijit/form/Button" data-dojo-props="onClick: function(){ ctrl.reset(); }">Reset</button> |
| 208 |
<button class="buttoncell" id="commit" type="button" data-dojo-type="dijit/form/Button" data-dojo-props="onClick: function(){ ctrl.commit(); }">Save</button> |
| 209 |
</div> |
| 210 |
|
| 211 |
<br/> |
| 212 |
|
| 213 |
|
| 214 |
<div class="fullrow" data-dojo-type="dojox/mvc/Group" data-dojo-props="target: at(ctrl,'cursor')"> |
| 215 |
<div> |
| 216 |
<h2>Verify the shipping address for: |
| 217 |
<div class="namecell" id="AddressNameOutput" data-dojo-type="dojox/mvc/Output" |
| 218 |
data-dojo-props="value: at('rel:', 'AddressName')"></div> |
| 219 |
</h2> |
| 220 |
</div> |
| 221 |
<div class="fullrow"> |
| 222 |
<div class="boldnamecell" data-dojo-type="dojox/mvc/Output" |
| 223 |
data-dojo-props="value: at('rel:', 'FullName')"></div> |
| 224 |
</div> |
| 225 |
<div class="fullrow"> |
| 226 |
<div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 227 |
data-dojo-props="value: at('rel:', 'AddressLine1')"></div> |
| 228 |
</div> |
| 229 |
<div class="hiderow" data-mvc-bindings="class: at('rel:', 'AddressLine2').direction(at.from).transform(ctrl.transformAddress2Class)"> |
| 230 |
<div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 231 |
data-dojo-props="value: at('rel:', 'AddressLine2')"></div> |
| 232 |
</div> |
| 233 |
<div class="fullrow"> |
| 234 |
<div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 235 |
data-dojo-props="exprchar:'%', value: at('rel:', 'City')">%{this.value},</div> |
| 236 |
<div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 237 |
data-dojo-props="value: at('rel:', 'State')"></div> |
| 238 |
<div class="namecell" data-dojo-type="dojox/mvc/Output" |
| 239 |
data-dojo-props="value: at('rel:', 'Zip')"></div> |
| 240 |
</div> |
| 241 |
</div> |
| 242 |
</div> |
| 243 |
</div> |
| 244 |
</body> |
| 245 |
</html> |