2

First of all excuse my low programming skills and for asking things you may find obvious. I'm learning by my own, without more help than online books and sites like this.

I'm reading the well known 'Developing Backbone Applications' by A. Osmani, trying to understand and to not letting things out.

I have doubts on the following lines:

(This is part of the views introduction, http://addyosmani.github.io/backbone-fundamentals/#application-view)

 initialize: function() { this.allCheckbox = this.$('#toggle-all')[0]; this.$input = this.$('#new-todo'); this.$footer = this.$('#footer'); this.$main = this.$('#main'); 

What are "this.allCheckbox", "$input", "$footer" and "$main" ?... Is he selecting already existing elements? Defining them? Why 'allCheckbox' doesn't start with '$' like the others?

Thanks for your help.

1 Answer 1

4

A $ prefixed variable typically refers to a jQuery object (as you can see, he is selecting DOM nodes to assign to those variables. That's just a convention.

The this.$() method runs a scoped jQuery selection. It finds matches for the query INSIDE the views $el element. This runs a bit faster than searching the whole page. He is indeed selecting elements that already exist, inside the $el of this view.

The allCheckbox doesn't have a $ prefixing it because by using [0] on an array of jQuery objects, you are actually at that point returning a raw DOM node, not a jQuery object. Using variable naming like that helps you a lot in an untyped language to know what methods might exist on that variable.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.