4

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.

4
  • 1
    What problems are you having? What are you expecting this code to do that it doesn't? Commented Jul 20, 2012 at 19:03
  • I want the alert('new method') to fire on btn.click instead of the alert('default function'). Commented Jul 20, 2012 at 19:05
  • I would prefer a structure like this: jsfiddle.net/FUBxq/2 - keep the callback function private and overriding it in the init() looks much cleaner to me. Commented Jul 20, 2012 at 19:33
  • @Christoph - Yes, as long as it's not something that might need to be changed later, that would be cleaner. Commented Jul 20, 2012 at 20:25

4 Answers 4

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

Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

it would prefer keeping the callback private and overriding it in the init function: jsfiddle.net/FUBxq/2
1
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();

Comments

1

When I separated out the prototype functions, and removed the return { Init: Init, Callback : Callback }; piece everything seems to work fine.

function Refinements() {}

Refinements.prototype.Init = function() {
    $('.btn').click(this.Callback);
};

Refinements.prototype.Callback = function() {
    alert('default function');
};


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

http://jsfiddle.net/Qyhrb/8/

Comments

1
Foobar = function() {
    this.callBack = function() {
        alert("Default method");
    };
}

Foobar.prototype = {
    Init: function() {
        var self = this;
        $(".btn").click(function() {
            self.callBack.call();
        });
    }
};

var foobar = new Foobar();
foobar.Init();
foobar.callBack = function() {
    alert("Boo!");
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.