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

Defining business logic with models and collections


Now, it is time to design business logic for the Billing Application using the MVC and OOP approaches.

In this recipe, we are going to define an internal structure for our application with model and collection objects. Although a model represents a single object, a collection is a set of models that can be iterated, filtered, and sorted.

Relations between models and collections in the Billing Application conform to the following scheme:

How to do it...

For each model, we are going to create two tables: one for properties and another for methods:

  1. We define BuyerModel properties.

    Name

    Type

    Required

    Unique

    id

    Integer

    Yes

    Yes

    name

    Text

    Yes

     

    address

    Text

    Yes

     

    phoneNumber

    Text

    No

     
  2. Then, we define SellerModel properties.

    Name

    Type

    Required

    Unique

    id

    Integer

    Yes

    Yes

    name

    Text

    Yes

     

    address

    Text

    Yes

     

    phoneNumber

    Text

    No

     

    taxDetails

    Text

    Yes

     
  3. After this, we define BankAccountModel properties.

    Name

    Type

    Required

    Unique

    id

    Integer

    Yes

    Yes

    beneficiary

    Text

    Yes

     

    beneficiaryAccount

    Text

    Yes

     

    bank

    Text

    Yes

     

    SWIFT

    Text

    Yes

     

    specialInstructions

    Text

    No

     
  4. We define InvoiceItemModel properties.

    Name

    Arguments

    Return Type

    Unique

    calculateAmount

    -

    Decimal

     
  5. Next, we define InvoiceItemModel methods.

    We don't need to store the item amount in the model, because it always depends on the price and the quantity, so it can be calculated.

    Name

    Type

    Required

    Unique

    id

    Integer

    Yes

    Yes

    deliveryDate

    Date

    Yes

     

    description

    Text

    Yes

     

    price

    Decimal

    Yes

     

    quantity

    Decimal

    Yes

     
  6. Now, we define InvoiceModel properties.

    Name

    Type

    Required

    Unique

    id

    Integer

    Yes

    Yes

    referenceNumber

    Text

    Yes

     

    date

    Date

    Yes

     

    bankAccount

    Reference

    Yes

     

    items

    Collection

    Yes

     

    comments

    Text

    No

     

    status

    Integer

    Yes

     
  7. We define InvoiceModel methods.

    The invoice amount can easily be calculated as the sum of invoice item amounts.

    Name

    Arguments

    Return Type

    Unique

    calculateAmount

     

    Decimal

     
  8. Finally, we define collections.

    In our case, they are InvoiceCollection, InvoiceItemCollection, BuyerCollection, and BankAccountCollection. They are used to store models of an appropriate type and provide some methods to add/remove models to/from the collections.

How it works...

Models in Backbone.js are implemented by extending Backbone.Model, and collections are made by extending Backbone.Collection. To implement relations between models and collections, we can use special Backbone extensions, which are described in the later chapters of this book.

See also

  • The Operating with model attributes recipe in Chapter 2, Models

  • The Creating a collection of models recipe in Chapter 3, Collections

To learn more about object properties, methods, and OOP programming in JavaScript, you can refer to the following resource:

https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript