Author: mhermanto
Date: Wed Mar 9 21:10:46 2011
New Revision: 1079995
URL: http://svn.apache.org/viewvc?rev=1079995&view=rev
Log:
Allow extensions to the common container by allowing other features to provide
their own namespace.
Much thanks to Ryan Baxter for this. Code reviewed in --
http://codereview.appspot.com/4260049/.
Modified:
shindig/trunk/features/src/main/javascript/features/container/container.js
shindig/trunk/features/src/test/javascript/features/container/container_test.js
Modified:
shindig/trunk/features/src/main/javascript/features/container/container.js
URL:
http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container/container.js?rev=1079995&r1=1079994&r2=1079995&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/container/container.js
(original)
+++ shindig/trunk/features/src/main/javascript/features/container/container.js
Wed Mar 9 21:10:46 2011
@@ -114,6 +114,8 @@ shindig.container.Container = function(o
*/
this.tokenRefreshTimer_ = null;
+ this.initializeMixins_();
+
this.preloadFromConfig_(config);
this.registerRpcServices_();
@@ -293,6 +295,17 @@ shindig.container.Container.prototype.rp
shindig.container.Container.prototype.onConstructed = function(opt_config) {};
+/**
+ * Adds a new namespace to the Container object. The namespace
+ * will contain the result of calling the function passed in.
+ * @param {string} namespace
+ * @param {function} func to call when creating the namespace
+ */
+shindig.container.Container.addMixin = function(namespace, func) {
+ shindig.container.Container.prototype.mixins_[namespace] = func;
+};
+
+
//
-----------------------------------------------------------------------------
// Valid JSON keys.
//
-----------------------------------------------------------------------------
@@ -420,6 +433,27 @@ shindig.container.ContainerRender.WIDTH
// Private variables and methods.
//
-----------------------------------------------------------------------------
+
+/**
+ * Adds the ability for features to extend the container with
+ * their own functionality that may be specific to that feature.
+ * @type {Object}
+ * @private
+ */
+shindig.container.Container.prototype.mixins_ = {};
+
+
+/**
+ * Called from the constructor to add any namespace extensions.
+ * @private
+ */
+shindig.container.Container.prototype.initializeMixins_ = function() {
+ for (var i in this.mixins_) {
+ this[i] = new this.mixins_[i](this);
+ }
+};
+
+
/**
* Add list of gadgets to preload list
* @param {Object} response hash of gadgets data
Modified:
shindig/trunk/features/src/test/javascript/features/container/container_test.js
URL:
http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/container/container_test.js?rev=1079995&r1=1079994&r2=1079995&view=diff
==============================================================================
---
shindig/trunk/features/src/test/javascript/features/container/container_test.js
(original)
+++
shindig/trunk/features/src/test/javascript/features/container/container_test.js
Wed Mar 9 21:10:46 2011
@@ -112,6 +112,38 @@ ContainerTest.prototype.testNewGadgetSit
this.assertTrue(container.sites_[2] != null);
};
+ContainerTest.prototype.testMixinViaPrototype = function() {
+ this.setupGadgetsRpcRegister();
+ shindig.container.Container.prototype.mixins_['test'] = function(context) {
+ return {
+ 'getSitesLength' : function() {
+ return context.sites_.length;
+ }
+ };
+ };
+ var container = new shindig.container.Container();
+ this.setupGadgetSite(1, {}, null);
+ container.newGadgetSite(null);
+ this.assertTrue(container.sites_[1] != null);
+ this.assertEquals(container.sites_.length, container.test.getSitesLength());
+};
+
+ContainerTest.prototype.testMixinViaAdd = function() {
+ this.setupGadgetsRpcRegister();
+ shindig.container.Container.addMixin('test2', function(context) {
+ return {
+ 'getSitesLength' : function() {
+ return context.sites_.length;
+ }
+ };
+ });
+ var container = new shindig.container.Container();
+ this.setupGadgetSite(1, {}, null);
+ container.newGadgetSite(null);
+ this.assertTrue(container.sites_[1] != null);
+ this.assertEquals(container.sites_.length, container.test2.getSitesLength());
+};
+
ContainerTest.prototype.setupGadgetSite = function(id, gadgetInfo,
gadgetHolder) {
var self = this;
shindig.container.GadgetSite = function() {