I am just going through some backbone tutorials and I have a general jQuery question that I have actually been wondering for a while.
Sometimes I see calls with a second parameter in the jQuery selector, for example $('ul', this.el)
.
What is the purpose of this second param in the selector? And I don't really mean in context of any backbone examples, just in general what is the purpose of passing the second param in the selector and why is it always an object that is passed there? I can't find any documentation on this.
$(this. el) creates a new instance of the jQuery/Zepto object. this. $el references a single instance of the former object.
el is just an identifier and it refers to an element, a DOM element, which is a convention in that library.
The meaning is identical to:
$(this.el).find('ul')
Internally, after a bunch of tests, jQuery figures out that it needs to flip it around to the above .find()
call, so it does, and starts over.
So providing the context as the second argument is just a slower way to do a .find()
.
Here it is in the source.
// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
return this.constructor( context ).find( selector );
}
...where this.constructor
is the $
function, context
is your second argument, and selector
is your first argument.
It narrows the search for a ul
tag within your view component's DOM element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With