The Models module
As we have done with the view-model, we are going to convert each model into a component and we are going to wrap it inside a module called Models
, the following steps:
Open the
models/product.js
file.Define our top-level module,
Shop
, and initialize it:var Shop; Shop = Shop || {};
Then create the
Models
namespace. It will be an object or the value it has before, in case it exists:Shop.Models = Shop.Models || {};
Define the product model with its dependencies. Remember, the first value is the product itself. This allows us to extend the model in case we use many files to define it. So, we define the product model as follows:
Shop.Models.Product = (function(){ })()
Pass the dependencies. This time you just need to use the Knockout dependency to use observables. Knockout is a global object and there is no need to add it to the dependencies, but it is a good practice to do it as in the following code:
Shop.Models.Product = (function (ko){ }(ko)
Finally, set the code we had in the...