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/model-classes.md
+41-4Lines changed: 41 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -113,23 +113,60 @@ created () {
113
113
114
114
`instanceDefaults(data, { store, Models })`
115
115
116
+
The `instanceDefaults` API was created in version 1.7 to prevent requiring to specify data for new instances created throughout the app. Depending on the complexity of the service's "business logic", it can save a lot of boilerplate. Notice that it is similar to the `setupInstance` method added in 2.0. The instanceDefaults method should ONLY be used to return default values for a new instance. Use `setupInstance` to handle other transformations on the data.
117
+
116
118
Starting with version 2.0, `instanceDefaults` must be provided as a function. The function will be called with the following arguments and should return an object of default properties for new instances.
117
119
118
120
-`data {Object}` - The instance data
119
121
- An `utils` object containing these props:
120
122
-`store` - The vuex store
121
-
-`Models {Object}` The `globalModels` object, which is the same as you'll find inside a component at `this.$FeathersVuex`.
123
+
-`models {Object}` The `globalModels` object, which is the same as you'll find inside a component at `this.$FeathersVuex`.
124
+
125
+
As an example, a User model class might look like this:
126
+
127
+
```js
128
+
instanceDefaults(data, { store, models }) {
129
+
return {
130
+
firstName:'',
131
+
lastName:'',
132
+
email:'',
133
+
password:'',
134
+
isAdmin:false
135
+
}
136
+
}
137
+
```
138
+
139
+
With the above attributes in place, you no longer have to manually specify any of the listed attributes. You can, however, provided data to replace them. Calling `new User({ firstName: 'Marshall' })` will create the instance with the `firstName` filled in, already.
140
+
141
+
One important note, the `isAdmin` attribute is specified in order to allow immediate binding in a form. You would pretty much NEVER allow specifying `isAdmin` from the client and storing it on the server. Attributes related to roles and app security should pretty much ALWAYS be written in hooks on the API server.
122
142
123
143
### setupInstance <Badgetext="2.0.0+" />
124
144
125
145
`setupInstance(data, { store, Models })`
126
146
127
-
A new `setupinstance` class method is now available in version 2.0. The function will be called with the following arguments and should return an object of default properties for new instances.
147
+
A new `setupinstance` class method is now available in version 2.0. This method allows you to transform the data and setup the final instance based on incoming data. For example, you can access the `models` object to reference other service Model classes and create data associations.
148
+
149
+
The function will be called with the following arguments and should return an object of default properties for new instances.
128
150
129
151
-`data {Object}` - The instance data
130
-
-An`utils` object containing these props:
152
+
-A`utils` object containing these props:
131
153
-`store` - The vuex store
132
-
-`Models {Object}` The `globalModels` object, which is the same as you'll find inside a component at `this.$FeathersVuex`.
154
+
-`models {Object}` The `globalModels` object, which is the same as you'll find inside a component at `this.$FeathersVuex`.
155
+
156
+
For an example of how you might use `setupInstance`, suppose we have two services: Users and Posts. Assume that the API request to get a user includes their `posts`, already populated on the data. The `instanceDefaults` allows us to convert the array of `posts` into an array of `Post` instances.
157
+
158
+
```js
159
+
// The setupInstance method on an imaginary User model.
160
+
setupInstance(data, { store, models }) {
161
+
if (data.posts) {
162
+
// Turn posts into an array of Post instances
163
+
data.posts=data.posts.map(p=>newmodels.Todo(p))
164
+
}
165
+
return data
166
+
}
167
+
```
168
+
169
+
With the above `setupInstance` method in place, each `User` instance now stores a direct reference to the `Post` records in the store.
0 commit comments