Skip to content

Commit ffd074b

Browse files
committed
Info about instanceDefaults and setupInstance
Closes #382
1 parent a6fd33f commit ffd074b

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

docs/model-classes.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,60 @@ created () {
113113

114114
`instanceDefaults(data, { store, Models })`
115115

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+
116118
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.
117119

118120
- `data {Object}` - The instance data
119121
- An `utils` object containing these props:
120122
- `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.
122142

123143
### setupInstance <Badge text="2.0.0+" />
124144

125145
`setupInstance(data, { store, Models })`
126146

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.
128150

129151
- `data {Object}` - The instance data
130-
- An `utils` object containing these props:
152+
- A `utils` object containing these props:
131153
- `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 => new models.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.
133170

134171
### on <Badge text="2.3.0+" />
135172

0 commit comments

Comments
 (0)