You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/technical-details/reference/resource-instance-binding.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
# Resource Class with Resource Instance Binding behavior
2
2
3
-
This document describes the legacy behavior of the Resource class using the instance binding behavior. It is recommended that [updated behavior](./resource.md) instead, but this legacy API is preserved for backwards compatibility.
3
+
This document describes the legacy behavior of the Resource class using the instance binding behavior. It is recommended that you use the [updated behavior of the Resource API](./resource.md) instead, but this legacy API is preserved for backwards compatibility.
4
4
## Resource Class
5
5
6
6
```javascript
7
7
exportclassMyExternalDataextendsResource {
8
8
static loadAsInstance =true;
9
-
asyncget() {
10
-
// fetch data from an external source, using our id
11
-
let response =awaitthis.fetch(this.id);
12
-
// do something with the response
13
-
}
14
-
put(data) {
9
+
asyncget() {
10
+
// fetch data from an external source, using our id
11
+
let response =awaitthis.fetch(this.id);
12
+
// do something with the response
13
+
}
14
+
put(data) {
15
15
// send the data into the external source
16
16
}
17
17
delete() {
@@ -138,8 +138,8 @@ The `query` argument is used to represent any additional query parameters that w
138
138
139
139
```javascript
140
140
put(data, query) {
141
-
let param1 = query?.get?.('param1'); // returns 'value'
142
-
...
141
+
let param1 = query?.get?.('param1'); // returns 'value'
142
+
...
143
143
}
144
144
```
145
145
@@ -340,18 +340,18 @@ This will define the function to use for a computed attribute. To use this, the
@@ -382,7 +382,7 @@ If the source resource implements subscription support, real-time invalidation c
382
382
This property can be set to force the direct URL request target to be mapped to the resource primary key. Normally, URL resource targets are parsed, where the path is mapped to the primary key of the resource (and decoded using standard URL decoding), and any query string parameters are used to query that resource. But if this is turned on, the full URL is used as the primary key. For example:
Copy file name to clipboardExpand all lines: docs/technical-details/reference/resource-migration.md
+53-53Lines changed: 53 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,10 @@ The updated Resource API is enabled on a per-class basis, by setting static `loa
6
6
* The `get` method (both static and instance) will directly return the record, a frozen enumerable object with direct properties, instead of a Resource instance.
7
7
* When instance methods are called, there will not be any record preloaded beforehand. And the resource instance will not have properties mapped to a record.
8
8
* All instance methods accept a `target`, an instance of `RequestTarget`, as the first argument, which identifies the target record or query.
9
-
* The `target` will have an `id` property identifying the target resource, along with target information. The `getId()` method will return `undefined`.
10
-
* The `target` will provide access to query parameters, search operators, and other directives.
11
-
* A `target` property of `checkPermission` indicates that a method should check the permission before of request before proceeding. The default instance methods provide the default authorization behavior.
12
-
* This supplants the need for `allowRead`, `allowUpdate`, `allowCreate`, and `allowDelete` methods, which shouldn't need to be used (and don't provide the id of the target record).
9
+
* The `target` will have an `id` property identifying the target resource, along with target information. The `getId()` method will return `undefined`.
10
+
* The `target` will provide access to query parameters, search operators, and other directives.
11
+
* A `target` property of `checkPermission` indicates that a method should check the permission before of request before proceeding. The default instance methods provide the default authorization behavior.
12
+
* This supplants the need for `allowRead`, `allowUpdate`, `allowCreate`, and `allowDelete` methods, which shouldn't need to be used (and don't provide the id of the target record).
13
13
* Any data from a POST, PUT, and PATCH request will be available in the second argument. This reverses the order of the arguments to `put`, `post`, and `patch` compared to the legacy Resource API.
14
14
* Context is tracked using asynchronous context tracking, and will automatically be available to calls to other resources. This can be disabled by setting `static explicitContext = true`, which can improve performance.
15
15
* The `update` method will return an `Updatable` object (instead of a Resource instance), which provides properties mapped to a record, but these properties can be updated and changes will be saved when the transaction is committed.
@@ -18,37 +18,37 @@ Here are examples of how to convert/upgrade to the non-instance binding Resource
18
18
Previous code with a `get` method:
19
19
```javascript
20
20
exportclassMyDataextendstables.MyData {
21
-
asyncget(query) {
22
-
let id =this.getId(); // get the id
23
-
if (query?.size>0) { // check number of query parameters
24
-
let idWithQuery = id +query.toString(); // add query parameters
25
-
let resource =awaittables.MyData.get(idWithQuery, this); // retrieve another record
26
-
resource.newProperty='value'; // assign a new value to the returned resource instance
27
-
return resource;
28
-
} else {
29
-
this.newProperty='value'; // assign a new value to this instance
30
-
returnsuper.get(query);
31
-
}
32
-
}
21
+
asyncget(query) {
22
+
let id =this.getId(); // get the id
23
+
if (query?.size>0) { // check number of query parameters
24
+
let idWithQuery = id +query.toString(); // add query parameters
25
+
let resource =awaittables.MyData.get(idWithQuery, this); // retrieve another record
26
+
resource.newProperty='value'; // assign a new value to the returned resource instance
27
+
return resource;
28
+
} else {
29
+
this.newProperty='value'; // assign a new value to this instance
30
+
returnsuper.get(query);
31
+
}
32
+
}
33
33
}
34
34
```
35
35
Updated code:
36
36
```javascript
37
37
exportclassMyDataextendstables.MyData {
38
-
static loadAsInstance =false; // opt in to updated behavior
39
-
asyncget(target) {
40
-
let id =target.id; // get the id
41
-
let record;
42
-
if (target.size>0) { // check number of query parameters
43
-
let idWithQuery =target.toString(); // this is the full target with the path query parameters
44
-
// we can retrieve another record from this table directly with this.get/super.get or with tables.MyData.get
45
-
record =awaitsuper.get(idWithQuery);
46
-
} else {
47
-
record =awaitsuper.get(target); // we can just directly use the target as well
48
-
}
49
-
// the record itself is frozen, but we can copy/assign to a new object with additional properties if we want
50
-
return { ...record, newProperty:'value' };
51
-
}
38
+
static loadAsInstance =false; // opt in to updated behavior
39
+
asyncget(target) {
40
+
let id =target.id; // get the id
41
+
let record;
42
+
if (target.size>0) { // check number of query parameters
43
+
let idWithQuery =target.toString(); // this is the full target with the path query parameters
44
+
// we can retrieve another record from this table directly with this.get/super.get or with tables.MyData.get
45
+
record =awaitsuper.get(idWithQuery);
46
+
} else {
47
+
record =awaitsuper.get(target); // we can just directly use the target as well
48
+
}
49
+
// the record itself is frozen, but we can copy/assign to a new object with additional properties if we want
50
+
return { ...record, newProperty:'value' };
51
+
}
52
52
}
53
53
```
54
54
Here is an example of the preferred approach for authorization:
@@ -83,34 +83,34 @@ Here is an example of how to convert/upgrade an implementation of a `post` metho
83
83
Previous code with a `post` method:
84
84
```javascript
85
85
exportclassMyDataextendstables.MyData {
86
-
asyncpost(data, query) {
87
-
let resource =awaittables.MyData.get(data.id, this);
@@ -393,7 +392,7 @@ If the source resource implements subscription support, real-time invalidation c
393
392
This property can be set to force the direct URL request target to be mapped to the resource primary key. Normally, URL resource targets are parsed, where the path is mapped to the primary key of the resource (and decoded using standard URL decoding), and any query string parameters are used to query that resource. But if this is turned on, the full URL is used as the primary key. For example:
394
393
```javascript
395
394
exportclassMyTableextendstables.MyTable {
396
-
static directURLMapping =true;
395
+
static directURLMapping =true;
397
396
}
398
397
```
399
398
```http request
@@ -646,7 +645,7 @@ export class CustomProduct extends Product {
646
645
post(target, data) {
647
646
let record = this.update(target);
648
647
record.name = data.name;
649
-
record.description = data.description;
648
+
record.description = data.description;
650
649
// both of these changes will be saved automatically as this transaction commits
0 commit comments