55
66# Resource Guide
77
8- A _resource_ is the data and meta data associated with a particular RESTful endpoint.
9-
10- You define _resources_ and register them with the data store. A _resource definition_ tells angular-data
11- about a particular _resource_, like what its root endpoint is and which attribute refers to the primary key of the
12- resource. A _resource definition_ can also specify validation functions to be executed before create and update
13- operations.
14-
15- See [defineResource(definition)](/documentation/api/api/DS.sync_methods:defineResource) for detailed API information.
8+ <page-list></page-list>
169
1710@doc overview
1811@id overview
19- @name Resource Guide
12+ @name Overview
2013@description
2114
2215A _resource_ is the data and meta data associated with a particular RESTful endpoint.
@@ -42,9 +35,9 @@ DS.defineResource('document');
4235With this definition the data store assumes the following:
4336
4437- Resource will be referred to as `"document"`
45- - The RESTful endpoint for this resource is `"/document"`
46- - The `idAttribute` (attribute that specifies the primary key) is `"id"`
47- - This resource does not use any validation
38+ - The RESTful endpoint for this resource is `DSProvider.defaults.baseUrl + "/document"`
39+ - The primary key is specified by the `"id"` property (or whatever is specified by `DSProvider.defaults.idAttribute`)
40+ - This resource has no custom lifecycle hooks (unless `DSProvider.defaults` has some lifecycle hooks defined)
4841
4942@doc overview
5043@id advanced
@@ -58,6 +51,7 @@ DS.defineResource({
5851 name: 'document',
5952 idAttribute: '_id',
6053 endpoint: 'documents',
54+ baseUrl: 'https://example.com/api',
6155 validate: function (attrs, cb) {
6256 if (!angular.isObject(attrs) {
6357 cb('Must be an object!');
@@ -71,6 +65,105 @@ DS.defineResource({
7165With this definition the data store understands the following:
7266
7367- Resource will be referred to as `"document"`
74- - The RESTful endpoint for this resource is `"/documents"`
75- - The `idAttribute` (attribute that specifies the primary key) is `"_id"`
76- - Before create/save operations the provided `validate` function must pass
68+ - The RESTful endpoint for this resource is `"https://example.com/api/documents"`
69+ - The primary key is specified by the `"_id"` property
70+ - Before create/save operations, the provided `validate` function is executed (and any lifecycle hooks defined in `DSProvider.defaults`)
71+
72+ See [DS.defineResource](/documentation/api/api/DS.sync_methods:defineResource) for the full resource definition specification.
73+
74+ @doc overview
75+ @id lifecycle
76+ @name Model Lifecycle Hooks
77+ @description
78+
79+ The following asynchronous operations support a model lifecycle:
80+
81+ ### DS.create()
82+
83+ - `beforeValidate` - Default: `noop`
84+ - `validate` - Default: `noop`
85+ - `afterValidate` - Default: `noop`
86+ - `beforeCreate` - Default: `noop`
87+ - `create` - Implementation provided by adapter
88+ - `afterCreate` - Default: `noop`
89+
90+ ### DS.save()
91+
92+ - `beforeValidate` - Default: `noop`
93+ - `validate` - Default: `noop`
94+ - `afterValidate` - Default: `noop`
95+ - `beforeUpdate` - Default: `noop`
96+ - `save` - Implementation provided by adapter
97+ - `afterUpdate` - Default: `noop`
98+
99+ ### DS.destroy()
100+
101+ - `beforeValidate` - Default: `noop`
102+ - `validate` - Default: `noop`
103+ - `afterValidate` - Default: `noop`
104+ - `beforeDestroy` - Default: `noop`
105+ - `destroy` - Implementation provided by adapter
106+ - `afterDestroy` - Default: `noop`
107+
108+ ### Define lifecycle hooks
109+ All lifecycle hooks will be executed according to the following signature:
110+ ```js
111+ exampleHook(resourceName, attrs, cb) {...}
112+ ```
113+
114+ `resourceName` is the name of the resource that `attrs` belong to, which is a reference to the object on which `create`,
115+ `save` or `destroy` was originally called.
116+
117+ `cb` is the callback function to be executed when the lifecycle hook is done. `cb` follows the Node style of callbacks,
118+ where the first passed argument will be the error, if any, and the second is the result. In the case of these lifecycle
119+ functions, `attrs` _is_ the result. So, `attrs` is available for inspection and/or modification, and then should be passed
120+ to `cb`. For example:
121+
122+ ```js
123+ validate(resourceName, attrs, cb) {
124+ console.log('hmm, looks good to me!');
125+ cb(null, attrs); // no error
126+ }
127+ ```
128+
129+ ```js
130+ validate(resourceName, attrs, cb) {
131+ console.log('something went wrong!');
132+ cb('some error'); // error!
133+ }
134+ ```
135+
136+ The lifecycle will be aborted if `cb` receives an error or an error is thrown.
137+
138+ Finally, model lifecycle hooks can be defined at the global level or per-resource. For example:
139+
140+ ```js
141+ angular.module('myApp', ['angular-data.DS'])
142+ .config(function (DSProvider) {
143+
144+ // Global definition
145+ DSProvider.defaults.beforeCreate = function (resourceName, attrs, cb) {
146+ console.log('Global beforeCreate');
147+ cb(null, attrs);
148+ };
149+
150+ })
151+ .run(function (DS) {
152+
153+ DS.defineResource({
154+ name: 'post',
155+
156+ // Local definition, overrides the global definition
157+ beforeCreate = function (resourceName, attrs, cb) {
158+ console.log('beforeCreate defined for ' + resourceName);
159+ cb(null, attrs);
160+ }
161+ });
162+
163+ // Will use the global definition
164+ DS.defineResource({
165+ name: 'comment'
166+ });
167+
168+ });
169+ ```
0 commit comments