Jump to content

_bind All


jimfog

Recommended Posts

I am reading this tutorial here on how to use backbone.js along with the jquery fullcalendar plugin to build an events calendar:http://blog.shinetech.com/2011/08/05/building-a-shared-calendar-with-backbone-js-and-fullcalendar-a-step-by-step-tutorial/

 

Ιf you see the code listed in the chapter named Bringing in Backbone...you will see in line 11 _.bindAll(this);

 

Despite having being read about the above in http://underscorejs.org/#bindAll I still cannot understand what it does.

I would expect that inside the parentheses some method/s would be listed, we find in it this.

 

Some help would be welcome here...I am trying to build an event(appointments to be precise) calendar here and know exactly what the code does is something that is needed.

Link to comment
Share on other sites

Maybe they're using an old version, because that documentation says that the second parameter is required. I'm just guessing here, but maybe if the second parameter is left out then it loops through all methods in the object and binds each one. Otherwise, it is supposed to be passed a list of method names to bind.

Link to comment
Share on other sites

Ok...I have one question that needs answering.

What is the reason for using bind anyway since to call an object method we just use the object notation(object.method).

 

This is a general question about binding and what it means in javascript overall.

Reading jquery documentation,it says, that it is NO LONGER the preferred method for attaching event handlers.

 

In fact I use $("#savepersonal").click(function(e)....type of code.

 

So, I am repeating the above question...what is the reason using it if we are going to call the method with the usual object dot notation

Link to comment
Share on other sites

In event handlers this will usually be changed to refer to the element that fired the event so that you can have easy access to it (often times when an element triggers a function through an event, that function will usually want to grab said element). As far as event handling goes, you can get away with not using this if you want to. but in other areas...

 

sometimes dot notation isn't enough, or sometimes you will actually want to use this. Sometimes certain functions change what the this reference refers to. setTimeout and setInterval both also change the this reference to the window delegating the function (honestly I don't know why it does that, it usually raises more troubles that way). Some classes have functions which are part of one this yet you may want a different object to have access to the this reference (one notable example is JQuery UI's tooltip will change the this reference to the target element in the content property/function, even though you say something like Dice.roll() the "this" usage inside the roll function will refer to the element, and not the dice. Bind will make sure roll uses the proper this).

 

and finally another example in a recent post I did brought up another use of bind, to use functions which belong to one Class on an object in a different class. I used Array.prototype.slice.bind(arguements) so that an Arguments object could use an Array's function as if it was an array, which in this case was a simple way to cast the Arguments object into an Array object. Slice uses the this keyword a lot in its function and since Arguments has a lot of the same properties as an Array, up to the point that Slice won't notice its not an array, Arguments can get away with using slice to make a copy of it as an array.

 

There are more uses of Bind, but suffice to say it tends to have more use in class functions that interact with different classes, rather than standalone event handlers, in my experience anyways.

Link to comment
Share on other sites

"Binding" is their term for calling a function to run in a certain scope that you specify. That has various names depending on who implements it. The ExtJS library refers to doing that as creating a delegate, for example. Like the documentation for the bind method says:

 

Bind a function to an object, meaning that whenever the function is called, the value of this will be the object.

 

That means that after you bind the object to the function, when the function is executed then the value of this will be the object that you bound to the function. That's all it's doing. The bindAll method just binds multiple methods to the same object. It's not about attaching event handlers or calling the function, it's about how the function runs once you call it. In jQuery they use the term "bind" to attach an event handler, but that's not how the underscore library uses the term.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...