Book Image

Backbone.js Cookbook

By : Vadim Mirgorod
Book Image

Backbone.js Cookbook

By: Vadim Mirgorod

Overview of this book

<p>There is no doubt that the superior rendering power of HTML5, thin-to-thick client transition and REST style communication created a new era in web development, replacing the outdated approach based on browser plugin technologies. Backbone.js allows developers to write lightweight, modular, and scalable JavaScript applications.<br /><br />Backbone.js Cookbook contains a series of recipes that provide practical, step-by-step solutions to the problems that may occur during frontend application development using an MVC pattern. You will learn how to build Backbone applications utilizing the power of popular Backbone extensions and integrating your app with different third party libraries. You will also learn how to fulfill the requirements of the most challenging tasks.<br /><br />The first chapter of the book introduces you to the MVC paradigm and teaches you how to architect rich Internet applications operating with basic concepts of Backbone.js. During the reading of this book you will learn how to solve challenging problems leveraging Backbone objects such as models, collections, views, routers, and so on.</p> <p><br />You learn how to use forms, layouts, templating engines, and other Backbone extensions, which will help you to complete specific features of your application. You will understand how to bind a model to a DOM element. You will see how perfectly Backbone.js integrates with third party libraries and frameworks such as jQuery, Zepto, Underscore.js, Require.js, Mustache.js, Twitter Bootstrap, jQueryMobile, PhoneGap and many others. This book will guide you in how to optimize and test your applications, create your own Backbone extensions, and share them with the open source community.</p> <p><br />With the help of Backbone.js Cookbook, you will learn everything you need to know to create outstanding rich Internet applications using the JavaScript programming language.</p>
Table of Contents (16 chapters)
Backbone.js Cookbook
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Designing an application with the MVC pattern


MVC is a design pattern that is widely used in user-facing software, such as web applications. It is intended for splitting data and representing it in a way that makes it convenient for user interaction. To understand what it does, understand the following:

  • Model: This contains data and provides business logic used to run the application

  • View: This presents the model to the user

  • Controller: This reacts to user input by updating the model and the view

There could be some differences in the MVC implementation, but in general it conforms to the following scheme:

Worldwide practice shows that the use of the MVC pattern provides various benefits to the developer:

  • Following the separation of the concerned paradigm, which splits an application into independent parts, it is easier to modify or replace

  • It achieves code reusability by rendering a model in different views without the need to implement model functionality in each view

  • It requires less training and has a quicker startup time for the new developers within an organization

To have a better understanding of the MVC pattern, we are going to design a Billing Application. We will refer to this design throughout the book when we are learning specific topics.

Our Billing Application will allow users to generate invoices, manage them, and send them to clients. According to the worldwide practice, the invoice should contain a reference number, date, information about the buyer and seller, bank account details, a list of provided products or services, and an invoice sum. Let's have a look at the following screenshot to understand how an invoice appears:

How to do it...

Let's follow the ensuing steps to design an MVC structure for the Billing Application:

  1. Let's write down a list of functional requirements for this application. We assume that the end user may want to be able to do the following:

    • Generate an invoice

    • E-mail the invoice to the buyer

    • Print the invoice

    • See a list of existing invoices

    • Manage invoices (create, read, update, and delete)

    • Update an invoice status (draft, issued, paid, and canceled)

    • View a yearly income graph and other reports

  2. To simplify the process of creating multiple invoices, the user may want to manage information about buyers and his personal details in the specific part of the application before he/she creates an invoice. So, our application should provide additional functionalities to the end user, such as the following:

    • The ability to see a list of buyers and use it when generating an invoice

    • The ability to manage buyers (create, read, update, and delete)

    • The ability to see a list of bank accounts and use it when generating an invoice

    • The ability to manage his/her own bank accounts (create, read, update, and delete)

    • The ability to edit personal details and use them when generating an invoice

    Of course, we may want to have more functions, but this is enough for demonstrating how to design an application using the MVC pattern.

  3. Next, we architect an application using the MVC pattern.

    After we have defined the features of our application, we need to understand what is more related to the model (business logic) and what is more related to the view (presentation). Let's split the functionality into several parts.

  4. Then, we learn how to define models.

    Models present data and provide data-specific business logic. Models can be related to each other. In our case, they are as follows:

    • InvoiceModel

    • InvoiceItemModel

    • BuyerModel

    • SellerModel

    • BankAccountModel

  5. Then, will define collections of models.

    Our application allows users to operate on a number of models, so they need to be organized into a special iterable object named Collection. We need the following collections:

    • InvoiceCollection

    • InvoiceItemCollection

    • BuyerCollection

    • BankAccountCollection

  6. Next, we define views.

    View present a model or a collection to the application user. A single model or collection can be rendered to be used by multiple views. The views that we need in our application are as follows:

    • EditInvoiceFormView

    • InvoicePageView

    • InvoiceListView

    • PrintInvoicePageView

    • EmailInvoiceFormView

    • YearlyIncomeGraphView

    • EditBuyerFormView

    • BuyerPageView

    • BuyerListView

    • EditBankAccountFormView

    • BankAccountPageView

    • BankAccountListView

    • EditSellerInfoFormView

    • ViewSellectInfoPageView

    • ConfirmationDialogView

  7. Finally, we define a controller.

    A controller allows users to interact with an application. In MVC, each view can have a different controller that is used to do following:

    • Map a URL to a specific view

    • Fetch models from a server

    • Show and hide views

    • Handle user input