Last active
December 11, 2015 06:08
-
-
Save flosse/4556581 to your computer and use it in GitHub Desktop.
Thoughts about the plugin API for scaleApp 0.4.x
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
/* ******************* | |
* Defining a plugin * | |
* ******************/ | |
/*** 1. Variant ***/ | |
var SandboxPlugin = (function(){ | |
function SandboxPlugin (sandbox){ | |
this.sb = sandbox; | |
}; | |
SandboxPlugin.prototype.foo = function(){ | |
return "bar"; | |
}; | |
return SandboxPlugin; | |
})(); | |
var CorePlugin = (function(){ | |
function CorePlugin (core){ | |
this.core = core; | |
}; | |
CorePlugin.prototype.x = function(){ | |
return "y"; | |
}; | |
return CorePlugin; | |
})(); | |
var BasePlugin = { | |
a: function(){ return "b";} | |
} | |
/*** 2. Variant ***/ | |
var SandboxPlugin = { | |
attach: function attach(options) { | |
this.foo = function(){ | |
return "bar"; | |
}; | |
}, | |
detach: function detach() {}, | |
init: function init(done) { | |
// do some async stuff | |
done(); | |
} | |
}; | |
var CorePlugin = { | |
attach: function attach(options) { | |
this.x = function(){ | |
return "y"; | |
}; | |
}, | |
detach: function detach() {}, | |
init: function init(done) {done();} | |
}; | |
var BasePlugin = { | |
attach: function attach(options) { | |
this.a = function(){ | |
return "b"; | |
} | |
}, | |
detach: function detach() {}, | |
init: function init(done) {done();} | |
}; | |
/* ******************* | |
* Register a plugin * | |
* ******************/ | |
/*** 1. Variant ***/ | |
var customPlugin = { | |
id: "custom", | |
sandbox: SandboxPlugin | |
core:CorePlugin | |
base:BasePlugin | |
}; | |
scaleApp.plugin.register(customPlugin); | |
/*** 2. Variant ***/ | |
// scaleApp.plugin.register(name, type, plugin); | |
scaleApp.plugin.register("custom", "sandbox", SandboxPlugin); | |
scaleApp.plugin.register("custom", "core", CorePlugin); | |
scaleApp.plugin.register("custom", "base", BasePlugin); | |
/*** 3. Variant ***/ | |
// register core plugin | |
scaleApp.use(BasePlugin); | |
// register app plugin | |
var app = new scaleApp.Core(); | |
app.use(CorePlugin); | |
// register sanbox plugin | |
// within the module | |
module = function(sanbox){ | |
sandbox.use(SandboxPlugin); | |
return ({ | |
init: function(){}, | |
destroy: function(){} | |
}); | |
}; | |
/* ******************* | |
* Plugins in action * | |
* ******************/ | |
scaleApp.a(); // returns "b" | |
var app = new scaleApp.Core(); | |
app.x(); // returns "y" | |
scaleApp.register("myModule", function(sanbox){ | |
return ({ | |
init: function(){ | |
sanbox.foo(); // returns "bar" | |
} | |
destroy: function(){ } | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment