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 = []; // events connect result |
---|
5 | |
---|
6 | var repeatmodel3 = null; //repeat view data model |
---|
7 | |
---|
8 | // delete an item |
---|
9 | var deleteResult = function(index){ |
---|
10 | var nextIndex = repeatmodel3.get("cursorIndex"); |
---|
11 | if(nextIndex >= index){ |
---|
12 | nextIndex = nextIndex-1; |
---|
13 | } |
---|
14 | repeatmodel3.model.splice(index, 1); |
---|
15 | repeatmodel3.set("cursorIndex", nextIndex); |
---|
16 | }; |
---|
17 | // show an item detail |
---|
18 | var setDetailsContext = function(index){ |
---|
19 | repeatmodel3.set("cursorIndex", index); |
---|
20 | }; |
---|
21 | // insert an item |
---|
22 | var insertResult = function(index){ |
---|
23 | if(index<0 || index>repeatmodel3.model.length){ |
---|
24 | throw Error("index out of data model."); |
---|
25 | } |
---|
26 | if((repeatmodel3.model[index].First=="") || |
---|
27 | (repeatmodel3.model[index+1] && (repeatmodel3.model[index+1].First == ""))){ |
---|
28 | return; |
---|
29 | } |
---|
30 | var data = {id:Math.random(), "First": "", "Last": "", "Location": "CA", "Office": "", "Email": "", "Tel": "", "Fax": ""}; |
---|
31 | repeatmodel3.model.splice(index+1, 0, new getStateful(data)); |
---|
32 | setDetailsContext(index+1); |
---|
33 | }; |
---|
34 | // get index from dom node id |
---|
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 | // repeat3 view init |
---|
46 | init: function(){ |
---|
47 | repeatmodel3 = this.loadedModels.repeatmodels3; |
---|
48 | var repeatDom = dom.byId('repeatWidget3'); |
---|
49 | var connectResult; |
---|
50 | connectResult = connect.connect(repeatDom, "button[id^=\"detail3\"]:click", function(e){ |
---|
51 | var index = getIndexFromId(e.target.id, "detail3"); |
---|
52 | setDetailsContext(index); |
---|
53 | }); |
---|
54 | _connectResults.push(connectResult); |
---|
55 | |
---|
56 | connectResult = connect.connect(repeatDom, "button[id^=\"insert3\"]:click", function(e){ |
---|
57 | var index = getIndexFromId(e.target.id, "insert3"); |
---|
58 | insertResult(index); |
---|
59 | }); |
---|
60 | _connectResults.push(connectResult); |
---|
61 | |
---|
62 | connectResult = connect.connect(repeatDom, "button[id^=\"delete3\"]:click", function(e){ |
---|
63 | var index = getIndexFromId(e.target.id, "delete3"); |
---|
64 | deleteResult(index); |
---|
65 | }); |
---|
66 | _connectResults.push(connectResult); |
---|
67 | }, |
---|
68 | // repeat3 view destroy |
---|
69 | destroy: function(){ |
---|
70 | var connectResult = _connectResults.pop(); |
---|
71 | while(connectResult){ |
---|
72 | connect.disconnect(connectResult); |
---|
73 | connectResult = _connectResults.pop(); |
---|
74 | } |
---|
75 | } |
---|
76 | }; |
---|
77 | }); |
---|