Time for action – implementing a reusable toolbar
Now let's create a new reusable Toolbar
object that encapsulates the code for a toolbar. That way we can also use it in our other applications later on. We will put it inside a new file called toolbar.js
. The constructor will take the root element of the toolbar wrapped in a jQuery object:
function Toolbar($toolbar) { var _this = this;
Remember how I said in Chapter 1, The Task at Hand that the this
pointer can cause problems when using event handlers with public methods? To get around that we will create a global _this
variable and set it to the object's this
so it's always available.
First we will implement the public methods. We have two methods that are used to notify the application that either a toolbar button or menu item has been clicked. In this object they are just placeholders. The client application will override them to implement custom behavior:
this.toolbarButtonClicked = function(action) { return false; }; this.menuItemClicked...