Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript prototype - adding a callback

I'm using the javascript revealing prototype pattern, and I want to add a callback. I'm trying something like:

http://jsfiddle.net/Qyhrb/2/

  var Refinements = function () { };

  Refinements.prototype = function () {
    Init = function () {
      $('.btn').click(Callback);
    },
    Callback = function(){
      alert('default function');
    };
    return { Init: Init, Callback : Callback };
  }();


   var refinements = new Refinements();
   refinements.Callback = function(){ alert('new method'); };
   refinements.Init();

Essentially what I want to do is pass a callback into the object and raise that callback when an event occurs.

like image 983
dhysong Avatar asked Feb 19 '23 19:02

dhysong


2 Answers

    Init = function () {
        var refinement = this;
        $('.btn').click(refinement.Callback || Callback);
    },

Fiddle

like image 178
Scott Sauyet Avatar answered Feb 28 '23 04:02

Scott Sauyet


var Refinements = function() {};

Refinements.prototype = function() {
  return {
    init: function() {
      $('.btn').click(this.callback);
    },
    callback: function() {
      alert('default function');
    }
  }
}();


var refinements = new Refinements();
refinements.callback = function() {
  alert('new method');
};
refinements.init();
like image 24
heavysixer Avatar answered Feb 28 '23 05:02

heavysixer