1 | define(["dojo/_base/lang","dojo/_base/declare", "dojo/Deferred", "dojo/_base/array", "dojo/dom-construct", "dijit/registry", "dojox/app/Controller"], |
---|
2 | function(lang, declare, Deferred, array, domConstruct, registry, Controller){ |
---|
3 | // module: |
---|
4 | // dojox/app/tests/layoutApp2/controllers/UnloadViewController |
---|
5 | // summary: |
---|
6 | // Used to Unload Views when they are no longer needed |
---|
7 | // Bind "unload-view" event on dojox/app application instance. |
---|
8 | // Do transition from one view to another view. |
---|
9 | |
---|
10 | return declare("dojox/app/tests/layoutApp2/controllers/UnloadViewController", Controller, { |
---|
11 | |
---|
12 | constructor: function(app){ |
---|
13 | this.app = app; |
---|
14 | // summary: |
---|
15 | // bind "app-transition" event on application instance. |
---|
16 | // |
---|
17 | // app: |
---|
18 | // dojox/app application instance. |
---|
19 | // events: |
---|
20 | // {event : handler} |
---|
21 | this.events = { |
---|
22 | "unload-view": this.unloadView |
---|
23 | }; |
---|
24 | }, |
---|
25 | |
---|
26 | unloadView: function(event){ |
---|
27 | // summary: |
---|
28 | // Response to dojox/app "unload-view" event. |
---|
29 | // If a view has children loaded the view and any children of the child will be unloaded. |
---|
30 | // |
---|
31 | // example: |
---|
32 | // Use trigger() to trigger "unload-view" event, and this function will response the event. For example: |
---|
33 | // | this.trigger("unload-view", {"parent":parent, "view":view, "callback":function(){...}}); |
---|
34 | // |
---|
35 | // event: Object |
---|
36 | // unloadView event parameter. It should be like this: {"parent":parent, "view":view, "callback":function(){...}} |
---|
37 | |
---|
38 | var parent = event.parent || this.app; |
---|
39 | var view = event.view || ""; |
---|
40 | var viewId = view.id; |
---|
41 | |
---|
42 | if(!parent || !view || !viewId){ |
---|
43 | console.warn("unload-view event for view with no parent or with an invalid view with view = ", view); |
---|
44 | return; |
---|
45 | } |
---|
46 | |
---|
47 | if(parent.selectedChildren[viewId]){ |
---|
48 | console.warn("unload-view event for a view which is still in use so it can not be unloaded for view id = " + viewId + "'."); |
---|
49 | return; |
---|
50 | } |
---|
51 | |
---|
52 | if(!parent.children[viewId]){ |
---|
53 | console.warn("unload-view event for a view which was not found in parent.children[viewId] for viewId = " + viewId + "'."); |
---|
54 | return; |
---|
55 | } |
---|
56 | |
---|
57 | this.unloadChild(parent, view); |
---|
58 | |
---|
59 | // call Load event callback |
---|
60 | if(event.callback){ |
---|
61 | event.callback(); |
---|
62 | } |
---|
63 | }, |
---|
64 | |
---|
65 | unloadChild: function(parent, viewToUnload){ |
---|
66 | // summary: |
---|
67 | // Unload the view, and all of its child views recursively. |
---|
68 | // Destroy all children, destroy all widgets, destroy the domNode, remove the view from the parent.children, |
---|
69 | // then destroy the view. |
---|
70 | // |
---|
71 | // parent: Object |
---|
72 | // parent of this view. |
---|
73 | // viewToUnload: Object |
---|
74 | // the view to be unloaded. |
---|
75 | |
---|
76 | for(var child in viewToUnload.children){ |
---|
77 | this.unloadChild(viewToUnload, child); // unload children then unload the view itself |
---|
78 | } |
---|
79 | if(viewToUnload.domNode){ |
---|
80 | // destroy all widgets, then destroy the domNode, then destroy the view. |
---|
81 | var widList = registry.findWidgets(viewToUnload.domNode); |
---|
82 | for(var wid in widList){ |
---|
83 | widList[wid].destroyRecursive(); |
---|
84 | } |
---|
85 | domConstruct.destroy(viewToUnload.domNode); |
---|
86 | } |
---|
87 | |
---|
88 | delete parent.children[viewToUnload.id]; // remove it from the parents children |
---|
89 | if(viewToUnload.destroy){ |
---|
90 | viewToUnload.destroy(); // call destroy for the view. |
---|
91 | } |
---|
92 | } |
---|
93 | }); |
---|
94 | }); |
---|