Solved

New plugin help

Posted on 2014-07-28
2
136 Views
Last Modified: 2014-08-27
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
Comment
Question by:tonelm54
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:tonelm54
Comment Utility
Thank you, Im still learning (early stages) so thank you for your help, Ill try and remember for future :-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now