Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call plugin method from external #11

Open
simonbuehler opened this issue Jun 12, 2015 · 2 comments
Open

call plugin method from external #11

simonbuehler opened this issue Jun 12, 2015 · 2 comments

Comments

@simonbuehler
Copy link

hi,

i wanted to combine the game with an countdown,so i have added the https://github.com/wimbarelds/TimeCircles Countdown which works fine. Now i'd like to add a handleTimeout() function in the plugin init function and call it like from the countdown callback. i changed the initialisation to

$.fn.quizyMemoryGame = function(optionsMethods) {
    if ( methods[optionsMethods] ) {
        return methods[ optionsMethods ].apply( this, arguments);
    } else if ( typeof optionsMethods === 'object' || ! optionsMethods ) {
         methods.init.apply(this, arguments);
       return methods.init;
    } else {
        $.error( 'Method ' +  optionsMethods + ' does not exist on jQuery.tooltip' );
    }
  }

to get a init function reference in the html page and do something like

var game = $('#my-memorygame').quizyMemoryGame(args); and then call it like game.handleTimeout() but its not defined ... any idea how to access a nested function inside the init function?

@simonbuehler
Copy link
Author

answering my own question

just added

      // timeout function
      $.fn.quizyMemoryGame.handleTimeout = function() {
        alert(numTotalClicks);
      }

before main init and now i works like $('#my-memorygame').quizyMemoryGame.handleTimeout();

and i reverted my changes made in the $.fn.quizyMemoryGame = function(optionsMethods) { part

@frenski
Copy link
Owner

frenski commented Jun 15, 2015

Hi @simonbuehler ,

Sorry for my late response.
I prefer the way you started doing it the first time.
However there is something wrong with it. The argument 'optionsMethods' you are passing can be in this case either an object with parameters to an init function or key for the methods array. A don't see where you define the methods array. And this is where you should define your handleTimeOut function.
If you have something like this:
var methods = {
init : function(options) {
// with this one you simply init the quizy plugin
},
handleTimeout : function( ) {
// here comes the code for your extra method
}
};
// Then comes the
$.fn.quizyMemoryGame.......
.....................

And then you should be able to call the plugin like this:
$('#my-memorygame').quizyMemoryGame(args) //init function
$('#my-memorygame').quizyMemoryGame('handleTimeout') // calls your function.

Does it make sense like this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants