backbone.js - Reference to HTML elements in view, a convention? -
i'm in progress of learning backbone.js , i'm using book developping backbone applications.
i have questions reference html elements , how stored. example:
initialize: function() { this.$input = this.$('#new-todo');
here html element id to-do stored in this.$input, why use $ in front of input, merely convention? if change this.$input this.input code works fine. find confusing because book states:
the view.$el property equivalent $(view.el) , view.$(selector) equivalent $(view.el).find(selector).
i think $(view.el) different (view.el).
how this.$input saved in backbone.js? if console.log it, produces:
object[input#new-todo property value = "" attribute value = "null"]
could give me insight? :)
using $ infront of variable name naming convention. helps developer in distinguishing variable holding jquery objects others.
view.$el helper variable provided backbone, can use directly, instead of explicitly forming jquery object. hence view.$el equivalent $(view.el).
view.$el assigned in setelement method:
setelement: function(element, delegate) { // code this.$el = element instanceof backbone.$ ? element : backbone.$(element); // code }
backbone.$ reference $ global variable exported jquery.
view.$(selector) method defined in view. it's definition same $(view.el).find(selector)
$: function(selector) { return this.$el.find(selector); }
Comments
Post a Comment