Last active
August 29, 2015 14:20
-
-
Save dhinesh03/4408820e78d14738f979 to your computer and use it in GitHub Desktop.
multi-tenancy workaround for mithril.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var z = (function (){ | |
//helpers | |
var cache = {}; | |
var target; | |
var type = {}.toString; | |
var noop = function() {}; | |
var retainer = { subtree : 'retain' }; | |
var treeNode = (function() { | |
treeNode=function(parent,componentName){ | |
this.cName=componentName; | |
this.parent=parent; | |
this.children={}; | |
return this; | |
}; | |
treeNode.prototype.addChild=function(componentName){ | |
var newChild = new treeNode(this,componentName); | |
this.children[componentName] = newChild; | |
return newChild; | |
}; | |
function _traverseUp (context,existingPath) { | |
var node; | |
var path = existingPath.slice(); | |
while (context.parent) { | |
if(!(path.indexOf(context.cName) > -1)) { | |
path.push(context.cName); | |
} | |
context = context.parent; | |
} | |
return path; | |
} | |
function _traverseDown (context,existingPath) { | |
var path =existingPath.slice(); | |
(function walkDown (node) { | |
var newContext; | |
if(!(path.indexOf(node.cName) > -1)) { | |
path.push(node.cName); | |
} | |
var obj = node.children | |
for (var o in obj) { | |
if(obj.hasOwnProperty(o)){ | |
newContext = obj[o]; | |
walkDown(newContext); | |
} | |
} | |
})(context); | |
return path | |
} | |
treeNode.prototype.getPath = function (existingPath) { | |
var path; | |
if (type.call(existingPath) === '[object Array]') { | |
path=existingPath.slice(); | |
} else { | |
path = []; | |
} | |
path = _traverseDown(this,path); | |
return _traverseUp(this,path); | |
}; | |
return treeNode | |
} )(); | |
var tenant=function(newCompTree,componentName, component,args) { | |
var controller = function() { | |
this.compTree=newCompTree; | |
var ctrl = (component.controller || noop).apply(this, args) || this | |
var originalOnunload = ctrl.onunload; | |
ctrl.onunload = function() { | |
//console.log(componentName+" cache removed") | |
if(cache[componentName]) delete cache[componentName] | |
if(originalOnunload) originalOnunload.apply(this, arguments) | |
} | |
return ctrl | |
} | |
var view = function(ctrl) { | |
if (arguments.length > 1) args = args.concat([].slice.call(arguments, 1)) | |
if(target === undefined || (type.call(target) === '[object Array]' && target.indexOf(componentName) > -1) || target === componentName || target === "all") | |
return component.view.apply(component, args.length ? [ctrl].concat(args) : [ctrl]) | |
else | |
return retainer | |
} | |
return {controller: controller, view: view} | |
} | |
return { | |
/* targetComponents may be an array of compTree elements / a compTree / all | |
(all/undefined) will trigger the full redraw | |
*/ | |
withTarget:function(targetComponents, callback) { | |
return function(e) { | |
//recursively construct the path | |
if(type.call(targetComponents) === '[object Array]') { | |
target = []; | |
for (var i = 0; i < targetComponents.length; i++) { | |
if(type.call(targetComponents[i]) === '[object Object]') { | |
target=targetComponents[i].getPath(target); | |
} | |
} | |
} | |
else if(type.call(targetComponents) === '[object Object]') { | |
target = targetComponents.getPath(); | |
} else { | |
target = targetComponents | |
} | |
callback.call(this, e) | |
} | |
}, | |
/* z.component takes 3 mandatory args and after any no of optional args same as m.component | |
1st arg => caller ctrl ,ie parent controller of the component | |
2nd arg => must be unique string that will refer the component instance | |
3rd arg => component | |
From 4th arg refer the m.component doc | |
*/ | |
component:function(parentCtrl,componentName,component){ | |
parentCtrl.compTree = parentCtrl.compTree || new treeNode(undefined,"root"); | |
var newCompTree = parentCtrl.compTree.addChild(componentName); | |
var args=[]; | |
if (arguments.length > 3) args = args.concat([].slice.call(arguments, 3)) | |
var mcomponent = m.component.apply(undefined,[tenant(newCompTree,componentName,component,args)].concat(args)); | |
mcomponent.view.$original = component.view; | |
return mcomponent; | |
}, | |
/* Accept only string (i.e., component name) / array of strings (i.e., array of component names) / all / undefined */ | |
setTarget:function(targets){ | |
target = targets; | |
}, | |
bindOnce:function(componentName,viewName,view) { | |
if(cache[componentName] === undefined) { | |
cache[componentName] = {}; | |
} | |
if (cache[componentName][viewName] === undefined) { | |
cache[componentName][viewName] = true | |
return view() | |
} | |
else return retainer | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example for the multi-tenancy gist