views:

31

answers:

1

Could someone explain why this.rel is giving back undefined and also what the regex is supposed to do. If this.rel is undefined the regex will not work either and is causing some kind of error because the alert underneath will not fire?

$.fn.facebox= function(settings) {
    init(settings)

    function clickHandler() {
      $.facebox.loading(true)
alert($(this).attr('rel'));
//alert(String(this.rel));
      // support for rel="facebox.inline_popup" syntax, to add a class
      // also supports deprecated "facebox[.inline_popup]" syntax
      var klass = this.rel.match(/facebox\[?\.(\w+)\]?/)
   alert(klass);
alert('ppp');
     // if (klass) klass = klass[1]

      //fillfaceboxFromHref(this.href, klass)
      return false
    }

    return this.click(clickHandler)
  }

thanks, richard

+1  A: 

an jQuery-Object has many properties, one of them is an array with the found elements.

You have to walk through this array and bind the event to these single elements:

  jQuery.fn.extend({
facebox: function() 
{
      //here this is a jQuery-object
  function clickHandler() 
      {
        //here this is a DOMElement
        alert($(this).attr('rel'));
        return false
      }

  return this.each(function() {
              var self = jQuery(this);
              self.click(clickHandler)
           });

}});

Dr.Molle
Is there a significant difference in doing it like this jQuery.fn.extend({facebox: function()
Richard
I know why this.rel is not working. It is because it is not native to an img tag. I tested it on a link tag and it worked.
Richard
about the differences: the significant I would like to say the option to add multiple methods. Furthermore $.extend gives you the ability to decide, if existing objects with the used name (if there are any) should be complete overwritten or only be extended. The assignment you use actually will always overwrite.
Dr.Molle
You mean that if there is one facebox open and to open another with an extra method, the first one could use this method also.I think the pattern is used because it allowes only one window opened at a time.I don't see fancybox using extend either, it just ads to the jquery namespace by using it's own namespace. I wanted to adopt this pattern also.
Richard
No, I does'nt mean that. The extending does not work with functions(what plugins usually are). It's more a kind of OOP, you can add new or change existing properties of an object. The other thing, that's maybe more interesting on creating own plugins is the optional 1st parameter of extend(). If set to false, you are safe from overwiting existing functions.
Dr.Molle