Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js - Todo Collection - What exactly is happening in this return statement?

Tags:

I'm looking back at the Backbone todo list and have a question about the collection.

Here is the code:

window.TodoList = Bacbone.Collection.extend({          model: Todo,          localStorage: new Store("todos"),          done: function() {             return this.filter(function(todo){return todo.get("done")})         },          remaining: function() {             return this.without.apply(this, this.done());         }        }) 

I understand everything that is going on here, except for the 'remaining' function.

The return statement: return this.without.apply(this, this.done()); is using a proxy to an underscore method - _.without

According to Underscore docs, here is what that is for:

without_.without(array, [*values]) Returns a copy of the array with all instances of the values removed. === is used for the equality test.

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); => [2, 3, 4]

So, I get that it is saying to return everything in the collection without a 'done' attribute with the value of 'true'.

What I don't understand is the 'apply' function that is being chained to it. That doesn't appear in the Backbone docs or the Underscore docs. At least not anywhere I can find it.

Can anyone explain in detail what is going on with those elements in the Return statement?

like image 960
PhillipKregg Avatar asked Feb 04 '12 00:02

PhillipKregg


People also ask

How does Backbone JS work?

BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications. When a model changes, it automatically updates the HTML of your application. BackboneJS is a simple library that helps in separating business and user interface logic.

Is Backbone JS still relevant?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

Is Backbone JS frontend or backend?

Backend Synchronization BackboneJS is use with the front-end and back-end systems, allows the synchronization with the backend to provide support to RESTful APIs.

What is El property of backbone JS view?

It defines which element to be used as the view reference. The this. el is created from the view's tagName, className, id and attributes properties, if specified. If not, el is an empty div.


1 Answers

this is referring to the collection.

apply is a method of javascript functions that allows you to set context of a method and send an array of values to the caller.

apply expects context as the first parameter then an array or array-like (such as arguments) which will be passed in as parameters the function.

You can do the same thing with .call except the 2nd+ params are comma separated.

apply and call are native to javascript.

So...

return this.without.apply(this, this.done());

the method this.done() returns an array, but uses the context of the collection and passes in a series of values to be ignored via the without method. Which in turn returns all todos that aren't done within the collection.

Example:

_.without([1,2,3,4],1,2); === _.without.call([], [1,2,3,4], 1, 2); 
like image 164
Trevor Avatar answered Sep 28 '22 22:09

Trevor