开发者

Javascript prototype callback

I saw this awesome post by John Resig on "Simple Class Instantiation": http://ejohn.org/blog/simple-class-instantiation/

So i started create scripts this way, but unfortunately it has caused some troubles and confusion.

I'm working on some kind of "custom dialog framework", where it should only be possible to view one dialog at a time, and if you dismiss one, the next appears. Let's call this dialog queue.

Secondly it should run proceed or cancel depending on what the users chooses. So we could use it like this:

var dialog = UIDialog();
dialog.proceed(function() {
    // Do stuff
});
dialog.cancel(function() {
    // Do stuff
});

I currently have this code: http://pastebin.com/sGyjArfA Right now the dialog queue seems to work, but I'm pretty lost on how i shou开发者_StackOverflowld make the callback thing work.


You need to store the callbacks in the dialog object, and then just call them at the appropriate times. E.g.,

UIDialog.prototype.proceed = function(callback) { this.cbProceed = callback; }

And then in the dialog logic that handles the proceed logic, just call the callback if it's set:

if (this.cbProceed && jQuery.isFunction(this.cbProceed))
  this.cbProceed();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜