| 1 |
define(["dojo/dom", "dojo/_base/connect", "dijit/registry", "dojox/mvc/at", "dojox/mvc/Repeat", "dojox/mvc/getStateful"], |
| 2 |
function(dom, connect, registry, at, Repeat, getStateful){ |
| 3 |
|
| 4 |
var _connectResults = []; |
| 5 |
|
| 6 |
var repeatmodel2 = null; |
| 7 |
|
| 8 |
|
| 9 |
var deleteResult = function(index){ |
| 10 |
var nextIndex = repeatmodel2.get("cursorIndex"); |
| 11 |
if(nextIndex >= index){ |
| 12 |
nextIndex = nextIndex-1; |
| 13 |
} |
| 14 |
repeatmodel2.model.splice(index, 1); |
| 15 |
repeatmodel2.set("cursorIndex", nextIndex); |
| 16 |
}; |
| 17 |
|
| 18 |
var setDetailsContext = function(index){ |
| 19 |
repeatmodel2.set("cursorIndex", index); |
| 20 |
}; |
| 21 |
|
| 22 |
var insertResult = function(index){ |
| 23 |
if(index<0 || index>repeatmodel2.model.length){ |
| 24 |
throw Error("index out of data model."); |
| 25 |
} |
| 26 |
if((repeatmodel2.model[index].First=="") || |
| 27 |
(repeatmodel2.model[index+1] && (repeatmodel2.model[index+1].First == ""))){ |
| 28 |
return; |
| 29 |
} |
| 30 |
var data = {id:Math.random(), "First": "", "Last": "", "Location": "CA", "Office": "", "Email": "", "Tel": "", "Fax": ""}; |
| 31 |
repeatmodel2.model.splice(index+1, 0, new getStateful(data)); |
| 32 |
setDetailsContext(index+1); |
| 33 |
}; |
| 34 |
|
| 35 |
var getIndexFromId = function(nodeId, perfix){ |
| 36 |
var len = perfix.length; |
| 37 |
if(nodeId.length <= len){ |
| 38 |
throw Error("repeat node id error."); |
| 39 |
} |
| 40 |
var index = nodeId.substring(len, nodeId.length); |
| 41 |
return parseInt(index); |
| 42 |
}; |
| 43 |
|
| 44 |
return { |
| 45 |
|
| 46 |
init: function(){ |
| 47 |
repeatmodel2 = this.loadedModels.repeatmodels2; |
| 48 |
var repeatDom = dom.byId('repeatWidget2'); |
| 49 |
var connectResult; |
| 50 |
connectResult = connect.connect(repeatDom, "button[id^=\"detail2\"]:click", function(e){ |
| 51 |
var index = getIndexFromId(e.target.id, "detail2"); |
| 52 |
setDetailsContext(index); |
| 53 |
}); |
| 54 |
_connectResults.push(connectResult); |
| 55 |
|
| 56 |
connectResult = connect.connect(repeatDom, "button[id^=\"insert2\"]:click", function(e){ |
| 57 |
var index = getIndexFromId(e.target.id, "insert2"); |
| 58 |
insertResult(index); |
| 59 |
}); |
| 60 |
_connectResults.push(connectResult); |
| 61 |
|
| 62 |
connectResult = connect.connect(repeatDom, "button[id^=\"delete2\"]:click", function(e){ |
| 63 |
var index = getIndexFromId(e.target.id, "delete2"); |
| 64 |
deleteResult(index); |
| 65 |
}); |
| 66 |
_connectResults.push(connectResult); |
| 67 |
}, |
| 68 |
|
| 69 |
destroy: function(){ |
| 70 |
var connectResult = _connectResults.pop(); |
| 71 |
while(connectResult){ |
| 72 |
connect.disconnect(connectResult); |
| 73 |
connectResult = _connectResults.pop(); |
| 74 |
} |
| 75 |
} |
| 76 |
}; |
| 77 |
}); |