Angular Model
Simple HATEOS-oriented persistence module for AngularJS.
Angular Model is a module that provides a simple way to bind client-side domain logic to JSON-based API resources.
By sticking to hypermedia design principles, Angular Model allows you to implement client applications that are cleanly decoupled from your server architecture.
Basic Usage
In your AngularJS application, include the JavaScript:
// your specific paths may vary
In your app configuration, state a dependency on Angular Model:
angular;
API documentation
The source code is documented using the ngdoc standard using gulp-ngdocs. A markdown version is browseable at /docs.
To generate documentation in HTML, run:
gulp ngdocs
This will output docs into the build/docs
directory. Then, using a server like ws
, start a local web server:
cd build/docsnpm install -g wsws
Then, you should be able to browse to http://localhost:8000 to view the API documentation for angular-model.
Configuration
Here is a quick reference guide to all the configuration settings you can pass to the model() constructor, which is documented in full in the API documentation. Each one is then described in detail later in this document, and in full in the source code in the src
directory.
Setting | Type | Description |
---|---|---|
url | string | API url that this model maps to |
defaults | object literal | Default values of attributes of instances of this model. Similar to properties in OOP. |
$instance | object literal | Instance methods available on each instance of this model. |
$class | object literal | Class methods available on this model. Similar to static methods in OOP. |
$collection | object literal | Collection |
Defaults
yourApp;
Here is an example of how the defaults get used:
var post = ;console; John Doe
Creating instances of your model
You can use angular-model ad-hoc to construct object instances:
// From defaultsvar post = ; // Specifying fieldsvar post = ; console; Steve Davis
Instance Methods
angular-model instances have instance methods, similar to objects in the OOP world.
Default instance methods
The following methods are available to every angular-model instance.
Function | Description |
---|---|
$save | Persist an instance to the API |
$delete | Tell the API to delete an instance |
$reload | Refresh an instance of a model from the API |
$revert | Reset the model to the state it was originally in when you first got it from the API |
$exists | Checks whether an object exists in the API, based on whether it has an identity URL. |
$dirty | Returns boolean - true if a model instance has been modified, else false. Opposite of $pristine. |
$pristine | Returns boolean - true if a model instance has unmodified, else false. Opposite of $dirty. |
$related | Hydrates the $links property of the instance. $links are used so that an instance can tell the client which objects are related to it. For example, a post may have an author object related to it. |
$modified | Returns a map of the properties that have been changed |
$hasRelated | Does an instance have a relation of name name ? |
You can see full details of these methods in the API documentation.
Custom instance methods
angular-model allows you to define instance methods on instances. This is similar to adding methods by extending a base class in the OOP world.
yourApp;
Example:
var post = ;console; foopng
Class methods
Default class methods
The following methods are available statically to angular-model:
Function | Description |
---|---|
all | Make a request to the API, based on the url configuration setting |
first | Given a query, get the first model instance from the API |
create | Create a new instance of the model. Defaults come from the defaults configuration setting. |
You can see full details of these methods in the API documentation.
Custom class methods
angular-model allows you to define class methods on instances. This is similar to static methods in the OOP world.
yourApp;
Example:
console; 'announcement' 'article'
Collection methods
You can use collection methods as well, so you can deal with a bunch of instances together. This allows you to have powerful and expressive methods on collections.
Default collection methods
The following methods are available statically to angular-model:
Function | Description |
---|---|
add | Saves the object with data |
remove | Find index and delete it from the API, then remove it from the collection |
You can see full details of these methods in the API documentation.
Custom collection methods
yourApp;
Example:
all;
Running unit tests
Install the test runner with npm:
npm install
You can then run the tests with gulp:
gulp
Tests can be found in the spec
directory of this project.
Related
You may wish to use Angular Scaffold, which is is a collection of convenience wrappers around angular-model collections. Really helpful for building your AngularJS application with angular-model.