Skip to content

Commit

Permalink
Ticket #108 : added the possibility to pass arguments to the plugin c…
Browse files Browse the repository at this point in the history
…onstructor (through the register function)
  • Loading branch information
obiot committed Jan 17, 2013
1 parent be676fd commit 981ea57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 4 additions & 4 deletions plugins/debug/debugPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
version : "0.9.5",

/** @private */
init : function() {
init : function(showKey, hideKey) {
// call the parent constructor
this.parent();

Expand Down Expand Up @@ -82,10 +82,10 @@

// enable the FPS counter
me.debug.displayFPS = true;

// bind the "S" and "H" keys
me.input.bindKey(me.input.KEY.S, "show");
me.input.bindKey(me.input.KEY.H, "hide");
me.input.bindKey(showKey || me.input.KEY.S, "show");
me.input.bindKey(hideKey || me.input.KEY.H, "hide");

// memory heap sample points
this.samples = [];
Expand Down
11 changes: 10 additions & 1 deletion src/plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
* @function
* @param {me.plugin.Base} plugin Plugin to instiantiate and register
* @param {String} name
* @param {Object} [args] all extra parameters will be passed to the plugin constructor
* @example
* // register a new plugin
* me.plugin.register(TestPlugin, "testPlugin");
Expand All @@ -120,8 +121,16 @@
throw ("melonJS: Plugin version mismatch, expected: "+ plugin.prototype.version +", got: " + me.version);
}

// get extra arguments
var _args = [];
if (arguments.length > 2) {
// store extra arguments if any
_args = Array.prototype.slice.call(arguments, 1);
}

// try to instantiate the plugin
me.plugin[name] = new plugin();
_args[0] = plugin;

This comment has been minimized.

Copy link
@parasyte

parasyte Feb 24, 2013

Collaborator

This doesn't look right! @obiot can you verify? I think it will actually break the first arg passed to the plugin constructor.

This comment has been minimized.

Copy link
@parasyte

parasyte Feb 24, 2013

Collaborator

Nope, disregard! :) I now notice the bind.apply on the next line. Scary JS voodoo!

This comment has been minimized.

Copy link
@melonjs

melonjs Feb 24, 2013

Collaborator

@parasyte this one is indeed tricky, but works flawlessly (i actually add some parameters to the debug plugin to test it)

me.plugin[name] = new (plugin.bind.apply(plugin, _args))();

// inheritance check
if (!(me.plugin[name] instanceof me.plugin.Base)) {
Expand Down

1 comment on commit 981ea57

@melonjs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was actually linked to ticket #85

Please sign in to comment.