Solved

New plugin help

Posted on 2014-07-28
2
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 40226080
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
ID: 40287313
Thank you, Im still learning (early stages) so thank you for your help, Ill try and remember for future :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

636 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