3

I've a custom plugin in jQuery,

jQuery.fn.dialog = function() {
    return this.each(function() {
        // Create dialog
    });
};

Now I want to add a function such that it can be called like this:-

$("#somediv").dialog.showMe();

I tried doing this:-

jQuery.fn.dialog.showMe = function() {
    $(this).show();
    // some code
};

But I think here this is not the div, so $(this).show() is not working.

How can I achieve this? Are there any better ways of adding functions to jQuery plugins?

4
  • dialog function has to return an object representing your dialog(s) collection then, you could append any methods to this dialog object's prototype ... Commented Apr 9, 2014 at 12:13
  • How can I do that from within this.each? Commented Apr 9, 2014 at 12:15
  • dialog.show(); doesn't work. Commented Apr 9, 2014 at 12:25
  • a comment: "But I think here this is not the div, so $(this).show() is not working." is the result of the selector your using Commented Apr 9, 2014 at 12:32

3 Answers 3

1

I coded a dialog factory base like what you are looking for ...

Feel free to ask if you have any question.

Test's fiddle here

// Dialog box factory base
(function ($) {

    // kind of private vars
    var dialog = function (opt, html) {
        this.domElt = $('<div />', {
            class: 'dialog-container'
        }).append(html);
        return this.init(opt);
    },dialogsStack = [];


    dialog.prototype = {
        init: function (options) {
            dialogsStack.push(this);
            this.params = $.extend(this.params, options, {
                dialogsIndex: (dialogsStack.length - 1)
            });
            console.log(this.params);
        },
        remove: function (all) {
            var dis = this;
            this.domElt.remove();
            if (dialogsStack.length)
                if (all) {
                    $.each(dialogsStack, function (i, el) {
                        el.domElt.remove();
                        // delete el;
                    });
                    dialogsStack = [];
                } else {
                    this.domElt.remove();
                    console.log('rm: ' + this.params.dialogsIndex);
                    dialogsStack.splice(this.params.dialogsIndex, 1);
                }
        },
        showMe: function () {
            this.domElt.appendTo('body');
        }
    };
    $.fn.dialog = function (options) {
        var t = this.eq(0);
        return new dialog(options, $('<div />').append(t).html());
    };
})(jQuery);
Sign up to request clarification or add additional context in comments.

2 Comments

Yep nevertheless it looks more like a generic template but seams to be a good summary of this
1

You should add new method inside dialog:

jQuery.fn.dialog = function() {
    this.showMe: function(){
        $(this).show();
    }
};

You can call this like:

var myDiv = $('#myDiv').dialog();
myDiv.showMe();

Or you can extend the current dialog and add method and use in same way above.

Comments

0

you missed the open and close the brace for the function . the function come like this $.fn.extend is Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.

<div id="someDiv"></div>

jquery

jQuery.fn.dialog = (function() {
    alert("a");
});

$("#someDiv").dialog();

Fiddle

1 Comment

The brace is missing because, I want function to be executed on top of the plugin like $("#somediv").dialog.showMe();, here showMe() is custom function.

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.