Closed gschammah closed 12 years ago
I don't think this should be baked in. In your initialize()
methods, why do you need the extra call to _initialize_elements()
? You could just have the one initialize()
or you could do something like this:
var MultiElementView = Backbone.View.extend({
constructor: function () {
Backbone.View.prototype.constructor.apply(this, arguments);
_.each(this.elements, function(value, key) {
if (_.isString(value)) {
this.elements[key] = this.$(value);
}
else {
// Do something with non-string elements.
}
}, this);
}
});
var SomeOtherView = MultiElementView.extend({
elements: {
a: "#a",
b: "#b"
}
});
var v = new SomeOtherView({ el: "#foo" });
console.log(v.elements); // a, b are now jQuery objects.
it means each time you'd create new jquery object and make sizzle dom search call as usually if you are using templating you woudln't even have access to the DOM of the view until it is rendered. You'd need to make it much smarter and cache the jquery object after first search....
anyways don't think it shouldn't be part of Backbone - if you need quick references to elements you should just define them in the render or initialize method IMO
I am doing something similar to what @TheCloudlessSky suggested right now, I created a Base view that converts my elements
jQuery selectors to jQuery objects, and extend all views from that one.
SpineJS has something like this that I think is pretty useful. Thanks for your comments
One more thing, in @TheCloudlessSky suggestion, is not possible to refer to any jQuery reference in the elements
object in the initialize
method, since you are calling the constructor, and the constructor invokes initialize()
, so elements
wont contain the jQuery references yet.
This is a duplicate of previous tickets -- take a look to see further conversation on the subject. This isn't something that should be built-in to backbone...
This is just a proposal for a common situation I am while working in a backbone project.
It is common for me to access my views' sub-elements many times. Here I put a simple example:
This works fine, but if the button id changes in the future, then I have to change that id in to places in my view (a real project will demand even more changes). So what I always end up doing is something like this:
For me this approach is fine, but I don't like to manually initialize every view every single time to store the jQuery reference to each of my view's sub-elements, and also is very easy to forget to do it.
I think the best approach would be for a Backbone view to have an
elements
object that works in a similar way than theevents
object.E.g.:
Then, before the
delegateEvents
function is called in Backbone.View constructor, we can replace the value of eachelements
key a jQuery object. I will request for a pull request soon. I would love to hear your comments.(Please note that this would be useful just in the cases where the HTML elements existed before the view instantiation)