1 | define(["./kernel", "./lang", "../on"], function(dojo, lang, on){ |
---|
2 | |
---|
3 | // module: |
---|
4 | // dojo/unload |
---|
5 | |
---|
6 | var win = window; |
---|
7 | |
---|
8 | var unload = { |
---|
9 | // summary: |
---|
10 | // This module contains the document and window unload detection API. |
---|
11 | |
---|
12 | addOnWindowUnload: function(/*Object|Function?*/ obj, /*String|Function?*/ functionName){ |
---|
13 | // summary: |
---|
14 | // registers a function to be triggered when window.onunload |
---|
15 | // fires. |
---|
16 | // description: |
---|
17 | // The first time that addOnWindowUnload is called Dojo |
---|
18 | // will register a page listener to trigger your unload |
---|
19 | // handler with. Note that registering these handlers may |
---|
20 | // destroy "fastback" page caching in browsers that support |
---|
21 | // it. Be careful trying to modify the DOM or access |
---|
22 | // JavaScript properties during this phase of page unloading: |
---|
23 | // they may not always be available. Consider |
---|
24 | // addOnUnload() if you need to modify the DOM or do |
---|
25 | // heavy JavaScript work since it fires at the equivalent of |
---|
26 | // the page's "onbeforeunload" event. |
---|
27 | // example: |
---|
28 | // | unload.addOnWindowUnload(functionPointer) |
---|
29 | // | unload.addOnWindowUnload(object, "functionName"); |
---|
30 | // | unload.addOnWindowUnload(object, function(){ /* ... */}); |
---|
31 | |
---|
32 | if (!dojo.windowUnloaded){ |
---|
33 | on(win, "unload", (dojo.windowUnloaded = function(){ |
---|
34 | // summary: |
---|
35 | // signal fired by impending window destruction. You may use |
---|
36 | // dojo.addOnWindowUnload() to register a listener for this |
---|
37 | // event. NOTE: if you wish to dojo.connect() to this method |
---|
38 | // to perform page/application cleanup, be aware that this |
---|
39 | // event WILL NOT fire if no handler has been registered with |
---|
40 | // addOnWindowUnload(). This behavior started in Dojo 1.3. |
---|
41 | // Previous versions always triggered windowUnloaded(). See |
---|
42 | // addOnWindowUnload for more info. |
---|
43 | })); |
---|
44 | } |
---|
45 | on(win, "unload", lang.hitch(obj, functionName)); |
---|
46 | }, |
---|
47 | |
---|
48 | addOnUnload: function(/*Object?|Function?*/ obj, /*String|Function?*/ functionName){ |
---|
49 | // summary: |
---|
50 | // registers a function to be triggered when the page unloads. |
---|
51 | // description: |
---|
52 | // The first time that addOnUnload is called Dojo will |
---|
53 | // register a page listener to trigger your unload handler |
---|
54 | // with. |
---|
55 | // |
---|
56 | // In a browser environment, the functions will be triggered |
---|
57 | // during the window.onbeforeunload event. Be careful of doing |
---|
58 | // too much work in an unload handler. onbeforeunload can be |
---|
59 | // triggered if a link to download a file is clicked, or if |
---|
60 | // the link is a javascript: link. In these cases, the |
---|
61 | // onbeforeunload event fires, but the document is not |
---|
62 | // actually destroyed. So be careful about doing destructive |
---|
63 | // operations in a dojo.addOnUnload callback. |
---|
64 | // |
---|
65 | // Further note that calling dojo.addOnUnload will prevent |
---|
66 | // browsers from using a "fast back" cache to make page |
---|
67 | // loading via back button instantaneous. |
---|
68 | // example: |
---|
69 | // | var afunc = function() {console.log("global function");}; |
---|
70 | // | require(["dojo/_base/unload"], function(unload) { |
---|
71 | // | var foo = {unload: function(){ console.log("unloading...");}, data: "mydata"}; |
---|
72 | // | unload.addOnUnload(afunc); |
---|
73 | // | unload.addOnUnload(foo, "unload"); |
---|
74 | // | unload.addOnUnload(foo, function(){console.log("", this.data);}); |
---|
75 | // | }); |
---|
76 | |
---|
77 | on(win, "beforeunload", lang.hitch(obj, functionName)); |
---|
78 | } |
---|
79 | }; |
---|
80 | |
---|
81 | dojo.addOnWindowUnload = unload.addOnWindowUnload; |
---|
82 | dojo.addOnUnload = unload.addOnUnload; |
---|
83 | |
---|
84 | return unload; |
---|
85 | |
---|
86 | }); |
---|