As we already know, AngularJS doesn’t come with an out of the box solution for data modeling. In the most abstract way, AngularJS lets us use JSON data as a model in the controller. As the time passed and my application grew, I realized that this modeling method isn’t powerful enough to fit my application’s needs. In this article I will present the way I dealt with data modeling in my AngularJS application.
As long as the book’s structure and methods are relevant only to one controller, all is fine and our work here is done. But as the application grows, there might be other controllers that will deal with books. Those controllers will sometimes need to fetch a book, update it, delete it or get it’s image url or availability. Therefore we have to share the behaviors of a book between controllers. In order to do this we will use a factory that returns the book’s behavior. Before writing this factory, I would like to mention here that we could make the factory return an object that contains helper methods for book (i.e. functions that get a book JSON and do what asked), but I prefer to use prototype for constructing a Book class, which I believe is the right choice:
Your new post is loading...