jQuery: The Write Less, Do More JavaScript Library

passing this

View: New views
6 Messages — Rating Filter:   Alert me  

passing this

by jney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hello,

i want to call a function $('#selection').parent.child()

with the following code :

jQuery.fn.extend({
  parent: {
    child: function(o){ doSomethingWith(selectedElement) }
  }
});

but i dont know how to pass the selected element through parent
(#selection) to child function.

regards.


Re: passing this

by JDempster :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


There is already a jQuery .parent() method.

Not quite sure what your trying todo. But each method is run in the
context of the selected elements.

e.g.

jQuery.fn.extend({
    test: function() {
        return this.each(function() {
           // do something with elements
        });
    }
});

would do something with all the selected elements via test. $
('div').test()

/James

On Oct 27, 8:49 pm, Jean-Sébastien <jeansebastien....@...>
wrote:

> Hello,
>
> i want to call a function $('#selection').parent.child()
>
> with the following code :
>
> jQuery.fn.extend({
>   parent: {
>     child: function(o){ doSomethingWith(selectedElement) }
>   }
>
> });
>
> but i dont know how to pass the selected element through parent
> (#selection) to child function.
>
> regards.


Re: passing this

by jney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


sorry, james i didn't see someone answer me.
what i want to do is ('div').set_of_plugins.choosen_plugin(). where
the first level (set_of_plugins) is kind of container (namespace) of
all my plugins. is it possible to do it?

On Oct 28, 5:20 pm, James Dempster <letss...@...> wrote:

> There is already a jQuery .parent() method.
>
> Not quite sure what your trying todo. But each method is run in the
> context of the selected elements.
>
> e.g.
>
> jQuery.fn.extend({
>     test: function() {
>         return this.each(function() {
>            // do something with elements
>         });
>     }
>
> });
>
> would do something with all the selected elements via test. $
> ('div').test()
>
> /James
>
> On Oct 27, 8:49 pm, Jean-Sébastien <jeansebastien....@...>
> wrote:
>
> > Hello,
>
> > i want to call a function $('#selection').parent.child()
>
> > with the following code :
>
> > jQuery.fn.extend({
> >   parent: {
> >     child: function(o){ doSomethingWith(selectedElement) }
> >   }
>
> > });
>
> > but i dont know how to pass the selected element through parent
> > (#selection) to child function.
>
> > regards.


Re: passing this

by Danny Wachsstock :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Interesting question. As far as I can tell (playing with things like $
(selector).(namespace.function)() and the like) it is not possible to
namespace plugins.
Danny

On Oct 28, 5:50 pm, Jean-Sébastien <jeansebastien....@...>
wrote:

> sorry, james i didn't see someone answer me.
> what i want to do is ('div').set_of_plugins.choosen_plugin(). where
> the first level (set_of_plugins) is kind of container (namespace) of
> all my plugins. is it possible to do it?
>
> On Oct 28, 5:20 pm, James Dempster <letss...@...> wrote:
>
> > There is already a jQuery .parent() method.
>
> > Not quite sure what your trying todo. But each method is run in the
> > context of the selected elements.
>
> > e.g.
>
> > jQuery.fn.extend({
> >     test: function() {
> >         return this.each(function() {
> >            // do something with elements
> >         });
> >     }
>
> > });
>
> > would do something with all the selected elements via test. $
> > ('div').test()
>
> > /James
>
> > On Oct 27, 8:49 pm, Jean-Sébastien <jeansebastien....@...>
> > wrote:
>
> > > Hello,
>
> > > i want to call a function $('#selection').parent.child()
>
> > > with the following code :
>
> > > jQuery.fn.extend({
> > >   parent: {
> > >     child: function(o){ doSomethingWith(selectedElement) }
> > >   }
>
> > > });
>
> > > but i dont know how to pass the selected element through parent
> > > (#selection) to child function.
>
> > > regards.


Re: passing this

by Danny Wachsstock :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I played with this (and was inspired by the code in
http://blog.jcoglan.com/2007/10/30/asynchronous-function-chaining-in-javascript)
and created a namespacing plugin:

(function($){
  $.namespace = function(ns, functions){
    $.fn[ns] = function() {return this.extend(functions)};
  };
  $.namespace('$', $.fn); // the default namespace
})(jQuery);

So now you can have a plugin:
$.fn.func = function(){alert('plugin'); return this'};

and create plugins in a namespace:
$.namespace ('mynamespace', {
  func: function() {alert('namespaced plugin'); return this;},
  otherfunc: function() {return this.css('color', 'yellow');}
});

And if you do
$('div').func(); // alerts 'plugin' -- no namespace
But
$('div').mynamespace().func(); // alerts 'namespaced plugin'
And
$('div').mynamespace().func().$().func(); // alerts 'namespaced
plugin', then resets to the normal jquery and alerts 'plugin'

Danny


On Oct 28, 5:50 pm, Jean-Sébastien <jeansebastien....@...>
wrote:
> sorry, james i didn't see someone answer me.
> what i want to do is ('div').set_of_plugins.choosen_plugin(). where
> the first level (set_of_plugins) is kind of container (namespace) of
> all my plugins. is it possible to do it?
>
> On Oct 28, 5:20 pm, James Dempster <letss...@...> wrote:
>




Re: passing this

by jney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


i just test it, it is just nice! and really usefull.

On Nov 2, 2:43 am, Danny <d.wac...@...> wrote:

> I played with this (and was inspired by the code inhttp://blog.jcoglan.com/2007/10/30/asynchronous-function-chaining-in-...)
> and created a namespacing plugin:
>
> (function($){
>   $.namespace = function(ns, functions){
>     $.fn[ns] = function() {return this.extend(functions)};
>   };
>   $.namespace('$', $.fn); // the default namespace
>
> })(jQuery);
>
> So now you can have a plugin:
> $.fn.func = function(){alert('plugin'); return this'};
>
> and create plugins in a namespace:
> $.namespace ('mynamespace', {
>   func: function() {alert('namespaced plugin'); return this;},
>   otherfunc: function() {return this.css('color', 'yellow');}
>
> });
>
> And if you do
> $('div').func(); // alerts 'plugin' -- no namespace
> But
> $('div').mynamespace().func(); // alerts 'namespaced plugin'
> And
> $('div').mynamespace().func().$().func(); // alerts 'namespaced
> plugin', then resets to the normal jquery and alerts 'plugin'
>
> Danny
>
> On Oct 28, 5:50 pm, Jean-Sébastien <jeansebastien....@...>
> wrote:
>
> > sorry, james i didn't see someone answer me.
> > what i want to do is ('div').set_of_plugins.choosen_plugin(). where
> > the first level (set_of_plugins) is kind of container (namespace) of
> > all my plugins. is it possible to do it?
>
> > On Oct 28, 5:20 pm, James Dempster <letss...@...> wrote: