Advertisement
Guest User

dojo destroy best practices

a guest
Apr 27th, 2012
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require(["dojo", "dojo/parser", "dijit/registry", "dojo/Stateful", "dojo/_base/connect",
  2. "dijit/form/Button", "dojox/widget/Toaster"],
  3. function (
  4.     dojo, parser, registry, Stateful, connect,
  5.     Button, Toaster
  6.     ) {
  7.  
  8.     connect.subscribe("/load", function () {
  9.         var stateful, t, tokens, toaster;
  10.  
  11.         stateful = new Stateful();
  12.         tokens = [];
  13.  
  14.         toaster = new Toaster({
  15.             positionDirection: 'bl-up',
  16.             duration: 500
  17.         });
  18.  
  19.         t = connect.subscribe("alert", toaster, "_handleMessage");
  20.         tokens.push(t);
  21.  
  22.         t = connect.subscribe("/unload", function () {
  23.             registry.toArray().forEach(function (w) {
  24.                 w.destroy();
  25.             });
  26.             dojo.forEach(tokens, connect.unsubscribe);
  27.             delete stateful;
  28.             stateful = undefined;
  29.             delete tokens;
  30.             tokens = undefined;
  31.             toaster.destroyRecursive();
  32.         });
  33.         tokens.push(t);
  34.  
  35.         t = connect.subscribe("CreateWijit", function () {
  36.             var w = new Button({
  37.                 id: "myButtonId",
  38.                 label: "myButtonTitle",
  39.                 onClick: function () {
  40.                     connect.publish("alert", { message: "myButton" });
  41.                 }
  42.             }, dojo.create("button", null, "workArea"));
  43.  
  44.             //dojo.byId("workArea").appendChild(w.domNode);
  45.             stateful.watch("myButtonTitle", function (t) {
  46.                 w.set("label", stateful.get(t));
  47.             });
  48.         });
  49.         tokens.push(t);
  50.  
  51.         t = connect.subscribe("DestroyWijit", function () {
  52.             var widgets = registry.findWidgets(dojo.byId("workArea"));
  53.             widgets.forEach(function (w) {
  54.                 w.destroyRecursive();
  55.             });
  56.         });
  57.         tokens.push(t);
  58.  
  59.         t = connect.subscribe("UpdateLabel", function () {
  60.             stateful.set("myButtonTitle", new Date().toLocaleTimeString());
  61.         });
  62.         tokens.push(t);
  63.  
  64.         new Button({
  65.             topic: "CreateWijit",
  66.             label: "Create Widget"
  67.         }, dojo.create("button", null, "controlContainer"));
  68.  
  69.         new Button({
  70.             topic: "DestroyWijit",
  71.             label: "Destroy Widget"
  72.         }, dojo.create("button", null, "controlContainer"));
  73.  
  74.         new Button({
  75.             topic: "UpdateLabel",
  76.             label: "Update Widget"
  77.         }, dojo.create("button", null, "controlContainer"));
  78.  
  79.         connect.publish("alert", { message: "Ready!" });
  80.  
  81.         registry.toArray().forEach(function (w) {
  82.             if (w.topic) {
  83.                 w.connect(w, "onClick", function () {
  84.                     connect.publish(w.topic);
  85.                     connect.publish("alert", { message: w.topic });
  86.                 });
  87.             }
  88.  
  89.         });
  90.     });
  91.  
  92.     parser.parse();
  93.  
  94.     window.unloadReload = function () {
  95.         connect.publish("/unload");
  96.         connect.publish("/load");
  97.     };
  98.     connect.publish("/load");
  99.  
  100. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement