function in module ur
Main factory function for angular-model
model(name[, options]);
Param | Type | Details |
---|---|---|
name | string | Name of the 'class', e.g. 'posts' |
options
(optional)
|
object | Config to initialise the model 'class' with. You can supply an object literal to configure your model here. |
ur.model | instance of angular-model for the 'class' identified by 'name' |
yourApp.config(function(modelProvider) {
modelProvider.model('posts', {
// configuration options
$instance: {
// custom instance functions
},
$class: {
// custom class functions
},
$collection: {
// custom collection functions
}
});
});
object in module ur
Methods available on model instances
You can use these when you have created or loaded a model instance, see the example
var post = model('posts').first({_id: 42});
console.log(post.name);
=> "Post with ID 42"
post.name = 'renamed';
post.$save();
You can specify custom instance methods:
yourApp.config(function(modelProvider) {
modelProvider.model('posts', {
$instance: {
$logo: function() {
return this.logo || '/logos/default.png';.
}
}
});
});
Delete an instance from the API
object | Promise from an API request |
post.$delete();
Returns boolean - true if a model instance is unmodified, else false. Inverse of $pristine.
boolean | true if a model instance is modified, else false. Inverse of $pristine. |
if (post.$pristine()) { console.log('It is just as it was when we got it from the API'); }
Checks whether an object exists in the API, based on whether it has an identity URL.
boolean | True if the identifier of this instance exists in the API |
if (post.$exists()) { console.log('It exists'); }
Does an instance have a relation of name name
?
Param | Type | Details |
---|---|---|
name | string | Name of the related property to check for |
boolean | true if a $link to |
if (post.$hasRelated('author')) { console.log('Post has an author'); }
Returns a map of the properties that have been changed
object | Map of the fields that have been changed from the $pristine version |
console.log(post.$modified());
Returns boolean - false if a model instance is unmodified, else true. Inverse of $dirty.
boolean | true if a model instance is unmodified, else false. Inverse of $dirty. |
if (post.$dirty()) { console.log('Post has been modified'); }
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.
object | Promise from the API |
console.log(post.links());
Refresh an instance of a model from the API
object | Promise from an API request |
post.$reload();
Reset the model to the state it was originally in when you first got it from the API
post.$revert();
Persist an instance to the API
Param | Type | Details |
---|---|---|
data
(optional)
|
object | Data to save to this model instance. Defaults to the result of |
object | Promise from an API request |
var post = model('posts').create({ name: 'some post' });
post.$save();
object in module ur
Methods available on the model class
Analogous to static methods in the OOP world
You can specify custom class methods:
yourApp.config(function(modelProvider) {
modelProvider.model('posts', {
$class: {
types: function() {
return ['announcement', 'article']
}
}
});
});
Retrieve collection of post instances from the API
Param | Type | Details |
---|---|---|
data
(optional)
|
object | Configuration of the request that will be sent to your API |
headers
(optional)
|
object | Map of custom headers to send to your API |
object | Promise from an API request |
model('posts').all().then(function(posts) {
console.log(posts.length);
});
=> 4
Creates an instance on of the model
Param | Type | Details |
---|---|---|
data
(optional)
|
object | Configuration of the instance that you are creating. Merged with any defaults specified when this model was declared. |
object | angular-model instance |
var post = model('Posts').create({});
Retrieve a single post instances from the API
Param | Type | Details |
---|---|---|
data
(optional)
|
object | Configuration of the request that will be sent to your API |
object | Promise from an API request |
model('posts').first({name: 'some post'}).then(function(post) {
console.log(post._id);
});
=> 42
object in module ur
Methods available on model collections
You can use collection methods to deal with a bunch of instances together. This allows you to have powerful and expressive methods on collections.
You can specify custom collection methods:
yourApp.config(function(modelProvider) {
modelProvider.model('posts', {
$collection: {
$hasArchived: function() {
return !angular.isUndefined(_.find(this, { archived: true }));
}
},
});
});
Saves the object
with data
Param | Type | Details |
---|---|---|
object | object | Object to persist data onto |
data
(optional)
|
object | Data to persist onto the object |
boolean | true if a $link to |
Find index
and delete it from the API, then remove it from the collection
Param | Type | Details |
---|---|---|
index | numberobject | Either the index of the item in the collection to remove, or the object itself, which will be searched for in the collection |
object | Promise from the API |
function in module ur
Get the model class factory
$get($http, $parse, $q);
Param | Type | Details |
---|---|---|
$http | object | |
$parse | object | |
$q | object |
object | The model service |
directive in module ur.model
angular-model will scan your page looking for <link rel="resources">
tags. It will use these to work out where your API endpoints are for your angular-model classes.
So, if you have a "class" Posts, you would define a link with an href pointing to the API endpoint for Posts. This should be a HATEOS-compliant API endpoint.
model
as element:
<link
rel="{string}"
name="{string}"
href="{string}">
</link>
Param | Type | Details |
---|---|---|
rel | string | Must be equal to "resource". |
name | string | The name of the angular-model "class" to use. |
href | string | Where should angular-model look for the API for this resource. |
<html ng-app="myApp">
<head>
<title>My Posts Application</title>
<link rel="resource" name="Posts" href="/api/posts">