Applet title updating tab; Revised applet code structure
authorDarren <darren@darrenwhitlen.com>
Sat, 15 Sep 2012 19:51:28 +0000 (20:51 +0100)
committerDarren <darren@darrenwhitlen.com>
Sat, 15 Sep 2012 19:51:28 +0000 (20:51 +0100)
client_backbone/dev/applet_settings.js
client_backbone/dev/model_applet.js
client_backbone/dev/view.js

index 4dc4e0ceeab1d45b7df463df67f7319664939f71..1edad219c9ff71964648adef1fa9566ad62b9fda 100644 (file)
@@ -1,23 +1,33 @@
-kiwi.applets.Settings = Backbone.View.extend({\r
-    events: {\r
-        'click .save': 'saveSettings'\r
-    },\r
+(function () {\r
+    var View = Backbone.View.extend({\r
+        events: {\r
+            'click .save': 'saveSettings'\r
+        },\r
 \r
-    initialize: function (options) {\r
-        this.$el = $($('#tmpl_applet_settings').html());\r
-        this.title = 'Settings';\r
-        window.s = this;\r
-    },\r
-    \r
-    saveSettings: function () {\r
-        var theme = $('.theme', this.$el).val(),\r
-            containers = $('#panels > .panel_container');\r
+        initialize: function (options) {\r
+            this.$el = $($('#tmpl_applet_settings').html());\r
+        },\r
+        \r
+        saveSettings: function () {\r
+            var theme = $('.theme', this.$el).val(),\r
+                containers = $('#panels > .panel_container');\r
 \r
-        // Clear any current theme\r
-        containers.removeClass(function (i, css) {\r
-            return (css.match (/\btheme_\S+/g) || []).join(' ');\r
-        });\r
+            // Clear any current theme\r
+            containers.removeClass(function (i, css) {\r
+                return (css.match (/\btheme_\S+/g) || []).join(' ');\r
+            });\r
 \r
-        if (theme) containers.addClass('theme_' + theme);\r
-    }\r
-});
\ No newline at end of file
+            if (theme) containers.addClass('theme_' + theme);\r
+        }\r
+    });\r
+\r
+\r
+\r
+    kiwi.applets.Settings = Backbone.Model.extend({\r
+        initialize: function () {\r
+            this.set('title', 'Settings');\r
+            this.view = new View();\r
+            window.s = this;\r
+        }\r
+    });\r
+})();
\ No newline at end of file
index b9610d558db220cded59a76b4f14df9be19f2943..36f548ab6eb999b4f0b664e085a3088a4ba2dd03 100644 (file)
@@ -3,6 +3,8 @@ kiwi.model.Applet = kiwi.model.Panel.extend({
     // differently than others\r
     applet: true,\r
 \r
+    loaded_applet: null,\r
+\r
     initialize: function (attributes) {\r
         // Temporary name\r
         var name = "applet_"+(new Date().getTime().toString()) + Math.ceil(Math.random()*100).toString();\r
@@ -16,14 +18,33 @@ kiwi.model.Applet = kiwi.model.Panel.extend({
     // Load an applet within this panel\r
     load: function (applet_object, applet_name) {\r
         if (typeof applet_object === 'object') {\r
-            this.set('title', applet_object.title || 'Something..');\r
-            this.view.$el.html('');\r
-            this.view.$el.append(applet_object.$el);\r
+            // Make sure this is a valid Applet\r
+            if (applet_object.get || applet_object.extend) {\r
+\r
+                // Try find a title for the applet\r
+                this.set('title', applet_object.get('title') || 'Unknown Applet');\r
+\r
+                // Update the tabs title if the applet changes it\r
+                applet_object.bind('change:title', function (obj, new_value) {\r
+                    this.set('title', new_value);\r
+                }, this);\r
+\r
+                // If this applet has a UI, add it now\r
+                this.view.$el.html('');\r
+                if (applet_object.view) {\r
+                    this.view.$el.append(applet_object.view.$el);\r
+                }\r
+\r
+                // Keep a reference to this applet\r
+                this.loaded_applet = applet_object;\r
+            }\r
 \r
         } else if (typeof applet_object === 'string') {\r
             // Treat this as a URL to an applet script and load it\r
             this.loadFromUrl(applet_object, applet_name);\r
         }\r
+\r
+        return this;\r
     },\r
 \r
     loadFromUrl: function(applet_url, applet_name) {\r
index e8e0eb0b44a3df89ab417ddb4e55d645a6cb6a4a..19238a4a1a74cfb2b041c18dd8554bb7cf5df323 100644 (file)
@@ -357,12 +357,17 @@ kiwi.view.Tabs = Backbone.View.extend({
         kiwi.app.view.doLayout();\r
     },\r
 \r
+    updateTabTitle: function (panel, new_title) {\r
+        $('span', panel.tab).text(new_title);\r
+    },\r
+\r
     panelAdded: function (panel) {\r
         // Add a tab to the panel\r
-        panel.tab = $('<li><span>' + (panel.get("title") || panel.get("name")) + '</span></li>');\r
+        panel.tab = $('<li><span>' + (panel.get('title') || panel.get('name')) + '</span></li>');\r
         panel.tab.data('panel_id', panel.cid)\r
             .appendTo(panel.isApplet() ? this.tabs_applets : this.tabs_msg);\r
 \r
+        panel.bind('change:title', this.updateTabTitle);\r
         kiwi.app.view.doLayout();\r
     },\r
     panelRemoved: function (panel) {\r