Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery on(); function

I'm learning Backbone and had some issues with the on()-function. But actually it's a very basic JavaScript question.

Why is it that the first line of code below works, and the second doesn't? Using the second line, the render-function is never triggered. Mind the brackets.

Works

this.collection.on( 'reset', this.render, this );

Fails

this.collection.on( 'reset', this.render(), this );
like image 937
ndequeker Avatar asked Jun 30 '12 13:06

ndequeker


People also ask

What is on () in jQuery?

The on() is an inbuilt method in jQuery which is used to attach one or more event handlers for the selected elements and child elements in the DOM tree. The DOM (Document Object Model) is a World Wide Web Consortium standard.

What is $( function () in jQuery?

jQuery (a library built on Javascript) has built in functions that generally required the DOM to be fully rendered before being called. The syntax for when this is completed is: $(document). ready(function() { });

Where do I write function in jQuery?

Answer: Use the syntax $. fn. myFunction=function(){} The syntax for defining a function in jQuery is little bit different from the JavaScript.

What does $( document .ready function () mean?

$( document ). ready() A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.


1 Answers

this.render() executes function (so in your case you are passing data returned from this function), whereas this.render is handler to function.

like image 55
Zbigniew Avatar answered Oct 21 '22 06:10

Zbigniew