I’m in a situation where I’m manipulating sub elements of a view and not sure if I’m properly handling them. This specific example is I have a carousel that has left and right buttons that disappear on certain events (for example, if you get to the end of the carousel in one direction). In order to manipulate these objects I’ve been doing something like this, but it doesn’t feel right:
$(this.el).find('.right-paddle').hide();
Or in some cases during instantiation call something like this:
this.rightPaddle = $(this.el).find('.right-paddle');
so that I can do something like this later:
this.rightPaddle.hide();
Is this bad form?
Why not? What’s wrong about it? Is it too much to write? Views are bound to ‘parent’ DOM elements and more often than not they have some children or elements within it that must be found in order to manipulate them. It’s perfectly valid
No. There is more than one way to do it. Do what makes your application more maintainable and readable and not necessarily strive for premature optimizations or hard-to-read code, unless it’s very performance critical (should still be easy to read though 🙂
Does this help alleviate your concern?