background preloader

Where to put model objects

Facebook Twitter

Modeling Data and State in Your AngularJS Application. Respect your data.

Modeling Data and State in Your AngularJS Application

Contain your state. Data and state are the foundation of your application. These two items should be absolutely respected. As you work through the AngularJS documentation, these two items are generally stored on the controllers. This works OK, but as your app grows beyond the “todo list” it quickly breaks down. I’ve written about this in the past, in the context of ActionScript 3 and the Robotlegs framework. What is a Model? A model notifies its associated views and controllers when there has been a change in its state. As the “M” in MVC, model classes encapsulate your application’s data and provide an API to access and manipulate that data. An example of this might be a shopping cart. In addition to controlling access to data, models maintain the state of your application. As you can see, data and state are intimately related. Models are portable. The model is the core of your application. Exploring the Code Introducing a Model to Store Data This is more like it.

The DCI Architecture: A New Vision of Object-Oriented Programming. The DCI Architecture: A New Vision of Object-Oriented Programmingby Trygve Reenskaug and James O.

The DCI Architecture: A New Vision of Object-Oriented Programming

CoplienMarch 20, 2009 Summary Object-oriented programming was supposed to unify the perspectives of the programmer and the end user in computer code: a boon both to usability and program comprehension. While objects capture structure well, they fail to capture system action. DCI is a vision to capture the end user cognitive model of roles and interactions between them.

Object oriented programming grew out of Doug Englebart's vision of the computer as an extension of the human mind. When a user approaches a GUI, he or she does two things: thinking and doing. We've been good at the mind-meld of structure Both object-oriented design and the Model-View-Controller (MVC) framework grew to support this vision. In some interfaces, this correspondence is obvious: if you create a circle on a PowerPoint® slide, the circle in your mind directly maps onto its representation in computer memory. Figure 1. Models and Services in Angular. Image from What's a Model?

Models and Services in Angular

What's a Service? I read over a post by Joel Hooks today called Modeling Data and State in Your AngularJS Application and the idea was put forward that you can clean up your Controllers by (basically) using Angular's service as a model. The idea is a good one: Clean Controllers are Godly Controllers but there's some confusion in the post that I think will end up spreading. Straight up-front: All services (service(), factory(), provider()) in Angular are SINGLETONS. There are so many jargon-filled terms disseminated throughout different approaches to software design... but I think we can agree that a model is representative of data (as opposed to a Domain Model which is all of your stuff under a big umbrella).

In the post above, Joel has a list of authors and a quote from each one - a simple data structure: One might consider this list to be two instances of a single Model, Author: Translating this to Joel's code: So far, so good. So What About a Model? Domain vs.