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:
For each model, we are going to create two tables: one for properties and another for methods:
We define BuyerModel properties.
Name
Type
Required
Unique
id
Integer
Yes
Yes
name
Text
Yes
address
Text
Yes
phoneNumber
Text
No
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
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
We define InvoiceItemModel properties.
Name
Arguments
Return Type
Unique
calculateAmount
-
Decimal
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
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
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
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.
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.
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