Basic View coding rules
Now, it is time to start coding the first View component. To help us through the process, we are going to lay two basic rules for View coding happiness:
- The View should encapsulate a DOM element
- Integrate Views with observers
So, let's see how they work individually.
The View should encapsulate a DOM element
As mentioned earlier, a View is the behavior associated with a DOM element, so it makes sense to have this element related to the View. A good pattern is to pass a CSS selector
in the View instantiation that indicates the element to which it should refer. Here is the spec for the NewInvestmentView
component:
describe("NewInvestmentView", function() { var view; beforeEach(function() { loadFixtures('NewInvestmentView.html'); view = new NewInvestmentView({ selector: '#new-investment' }); }); });
In the constructor function at the NewInvestmentView.js file, it uses jQuery to get the element for this selector and...