• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

New plugin help

Im trying to create my first plugin (to take a very messy code and hopefully tidy it up), however Im getting very stuck with the documentation.

What Im trying to do is create a plugin by:-
    var $newDialogue = $.tcDialogue({
        modal: true,
        draggable: true
    });
    $newDialogue.openURL("www.google.co.uk");

Open in new window


Which after I import my ''messy working" code will display www.google.co.uk (for example) in my own dialogue box (dont like the JQuery UI one), so Ive written the following:-
(function($){
    $.tcDialogue = function(el, openURL($strURL), showHTML($strHTML), closeDialoge, options){
        var base = this;
        
        base.$el = $(el);
        base.el = el;
        
        base.$el.data("tcDialogue", base);
        
        base.init = function(){
            base.openURL($strURL) = function ($strURL) {
                alert($strURL);
            };
            base.showHTML($strHTML) = showHTML($strHTML);
            base.closeDialoge = closeDialoge;
            
            base.options = $.extend({},$.tcDialogue.defaultOptions, options);
            
        };
        base.init();
    };
    
    $.tcDialogue.defaultOptions = {
        modal: true,
        draggable: true
    };
    
    $.fn.tcDialogue = function(openURL($strURL), showHTML($strHTML), closeDialoge, options){
        return this.each(function(){
            (new $.tcDialogue(this, openURL($strURL), showHTML($strHTML), closeDialoge, options));
        });
    };
    
})(jQuery);

Open in new window


But cant seem to get it to display the alert in, Ive spent a few hours on this and cannot see my mistake, Im now loosing my temper with it, and looking at just using my 'messy' code just to continue with the project, but hopefully an expert can spot my mistake and point me in the right direction. Ive uploaded the code to http://jsfiddle.net/YXsbZ/ in the hope an expert can show me my mistake.

Thank you
0
tonelm54
Asked:
tonelm54
1 Solution
 
Ioannis ParaskevopoulosCommented:
I have seen several issues with your code. I will try to list them here:

1. Wrong format of function params

Check the following line which initializes a function:
$.tcDialogue = function(el, openURL($strURL), showHTML($strHTML), closeDialoge, options)

Open in new window

It seems as you are passing a function call as a parameter. Pass the parameters themselves instead:
$.tcDialogue = function(el, $strURL, $strHTML, closeDialoge, options)

Open in new window

The same applies to the following:
$.fn.tcDialogue = function(openURL($strURL), showHTML($strHTML), closeDialoge, options)

Open in new window

Should be:
$.fn.tcDialogue = function($strURL, $strHTML, closeDialoge, options)

Open in new window


2. Wrong function calls

You are trying to call a function passing results of void functions as params:
$.tcDialogue(this, openURL($strURL), showHTML($strHTML), closeDialoge, options)

Open in new window

Try the following:
$.tcDialogue(this, $strURL, $strHTML, closeDialoge, options)

Open in new window


3. Wrong methods declaration

You are trying to declare object methods like:
           
base.openURL($strURL) = function ($strURL) {
    alert($strURL);
};

Open in new window

The left hand should not have parameters:
           
base.openURL= function ($strURL) {
    alert($strURL);
};

Open in new window

Also, the argument passed in the function will mask the property of the object with the same name. So if you intend to just open the url described in the object property then just use:
           
base.openURL= function () {
    alert($strURL);
};

Open in new window

Or if you intend to pass an actual parameter then do it like this:
           
base.openURL= function (url) {
    alert(url);
};

Open in new window


Check out this fork of your fiddle

I have commended out the code:
//base.showHTML = showHTML(strHTML);

Open in new window

I did not know what you wanted there.

Giannis
0
 
tonelm54Author Commented:
Thank you, Im still learning (early stages) so thank you for your help, Ill try and remember for future :-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now