Skip to content

Commit f56c00d

Browse files
Merge pull request #212 from fratzinger/patch-1
Patch 1
2 parents 4205f83 + 6250341 commit f56c00d

File tree

3 files changed

+66
-43
lines changed

3 files changed

+66
-43
lines changed

docs/service-module.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ Each service comes loaded with the following default state:
5252
{
5353
ids: [],
5454
keyedById: {}, // A hash map, keyed by id of each item
55+
copiesById: {}, // objects cloned with Model.clone()
5556
currentId: undefined, // The id of the item marked as current
5657
copy: undefined, // A deep copy of the current item
5758
idField: 'id',
5859
servicePath: 'v1/todos' // The full service path
5960
autoRemove: false, // Indicates that this service will not automatically remove results missing from subsequent requests.
61+
enableEvents: true, // Listens to socket.io events when available
62+
addOnUpsert: false, // Add new records pushed by 'updated/patched' socketio events into store, instead of discarding them
63+
diffOnPatch: false, // Only send changed data on patch
64+
skipRequestIfExists: false, // For get action, if the record already exists in store, skip the remote request
65+
preferUpdate: false, // When true, calling model.save() will do an update instead of a patch.
6066
replaceItems: false, // When set to true, updates and patches will replace the record in the store instead of merging changes
6167
paginate: false, // Indicates if pagination is enabled on the Feathers service.
6268

@@ -86,11 +92,17 @@ The following attributes are available in each service module's state:
8692

8793
- `ids {Array}` - an array of plain ids representing the ids that belong to each object in the `keyedById` map.
8894
- `keyedById {Object}` - a hash map keyed by the id of each item.
95+
- `copiesById {Object}` - objects cloned with Model.clone()
8996
- `currentId {Number|String}` - the id of the item marked as current.
9097
- `copy {Object}` - a deep copy of the current item at the moment it was marked as current. You can make changes to the copy without modifying the `current`. You can then use the `commitCopy` mutation to save the changes as the `current` or `rejectCopy` to revert `copy` to once again match `current`. You may prefer to use the new [clone API]() for [managing multiple copies with model instances](./common-patterns.md#Multiple-Copies).
9198
- `servicePath {String}` - the full service path, even if you alias the namespace to something else.
9299
- `modelName {String}` - the key in the $FeathersVuex plugin where the model will be found.
93-
- `autoRemove {Boolean` - indicates that this service will not automatically remove results missing from subsequent requests. Only use with feathers-rest. Default is false.
100+
- `autoRemove {Boolean}` - indicates that this service will not automatically remove results missing from subsequent requests. Only use with feathers-rest. Default is false.
101+
- `enableEvents {Boolean}` - Listens to socket.io events when available
102+
- `addOnUpsert {Boolean}` - Add new records pushed by 'updated/patched' socketio events into store, instead of discarding them
103+
- `diffOnPatch {Boolean}` - Only send changed data on patch
104+
- `skipRequestIfExists {Boolean}` - For get action, if the record already exists in store, skip the remote request
105+
- `preferUpdate {Boolean}`, // When true, calling model.save() will do an update instead of a patch.
94106
- `replaceItems {Boolean}` - When set to true, updates and patches will replace the record in the store instead of merging changes. Default is false
95107
- `idField {String}` - the name of the field that holds each item's id. *Default: `'id'`*
96108
- `paginate {Boolean}` - Indicates if the service has pagination turned on.
@@ -152,15 +164,21 @@ Removes a single item. `item` can be
152164
Removes the passed in items or ids from the store.
153165
- `items {Array}` - An array of ids or of objects with ids that will be removed from the data store.
154166

155-
### `setCurrent(state, item)`
167+
### `setCurrent(state, itemOrId)`
156168
- `item {Number|String|Object}` - the object with id to be set as the current item, or the id of the object in the store that should become the `current` item. Setting the `current` item or id also create the deep-cloned `copy`.
157169

158-
### `commitCopy(state)`
170+
### `createCopy(state, id)`
171+
Creates a copy of the record with the passed-in id, stores it in copiesById
172+
173+
### `commitCopy(state, id)`
159174
Saves changes from the `copy` to the `current` item.
160175

161-
### `rejectCopy(state)`
176+
### `rejectCopy(state, id)`
162177
Re-copies the data from `current` to `copy`, restoring the original copy.
163178

179+
### `clearCopy(state, id)`
180+
Removes the copy from copiesById
181+
164182
### `clearCurrent(state)`
165183
Clears the `current` item, which also clears the copy.
166184

@@ -170,6 +188,10 @@ Clears the `list`, excepting the `current` item.
170188
### `clearAll(state)`
171189
Clears all data from `ids`, `keyedById`, and `currentId`
172190

191+
### `updatePaginationForQuery(state, { qid, response, query })`
192+
Stores pagination data on state.pagination based on the query identifier (qid)
193+
The qid must be manually assigned to `params.qid`
194+
173195
### Mutations for Managing Pending State
174196
The following mutations are called automatically by the service actions, and will rarely, if ever, need to be used manually.
175197
- `setFindPending(state)` - sets `isFindPending` to `true`

src/service-module/mutations.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,6 @@ export default function makeServiceMutations (servicePath, { debug, globalModels
145145
}
146146
},
147147

148-
clearAll (state) {
149-
state.ids = []
150-
state.currentId = null
151-
state.copy = null
152-
state.keyedById = {}
153-
},
154-
155-
clearList (state) {
156-
let currentId = state.currentId
157-
let current = state.keyedById[currentId]
158-
159-
if (currentId && current) {
160-
state.keyedById = {
161-
[currentId]: current
162-
}
163-
state.ids = [currentId]
164-
} else {
165-
state.keyedById = {}
166-
state.ids = []
167-
}
168-
},
169-
170148
setCurrent (state, itemOrId) {
171149
const { idField } = state
172150
const Model = globalModels.byServicePath[servicePath]
@@ -190,13 +168,6 @@ export default function makeServiceMutations (servicePath, { debug, globalModels
190168
state.copy = null
191169
},
192170

193-
// Removes the copy from copiesById
194-
clearCopy (state, id) {
195-
const newCopiesById = Object.assign({}, state.copiesById)
196-
delete newCopiesById[id]
197-
state.copiesById = newCopiesById
198-
},
199-
200171
// Creates a copy of the record with the passed-in id, stores it in copiesById
201172
createCopy (state, id) {
202173
const current = state.keyedById[id]
@@ -211,8 +182,8 @@ export default function makeServiceMutations (servicePath, { debug, globalModels
211182
}
212183
},
213184

214-
// Resets the copy to match the original record, locally
215-
rejectCopy (state, id) {
185+
// Deep assigns copy to original record, locally
186+
commitCopy (state, id) {
216187
const isIdOk = checkId(id, undefined, debug)
217188
const current = isIdOk ? state.keyedById[id] : state.keyedById[state.currentId]
218189
const Model = globalModels.byServicePath[servicePath]
@@ -224,11 +195,13 @@ export default function makeServiceMutations (servicePath, { debug, globalModels
224195
copy = Model.copiesById[id]
225196
}
226197

227-
_merge(copy, current)
228-
},
198+
updateOriginal(copy, current)
229199

230-
// Deep assigns copy to original record, locally
231-
commitCopy (state, id) {
200+
// Object.assign(current, copy)
201+
},
202+
203+
// Resets the copy to match the original record, locally
204+
rejectCopy (state, id) {
232205
const isIdOk = checkId(id, undefined, debug)
233206
const current = isIdOk ? state.keyedById[id] : state.keyedById[state.currentId]
234207
const Model = globalModels.byServicePath[servicePath]
@@ -240,9 +213,36 @@ export default function makeServiceMutations (servicePath, { debug, globalModels
240213
copy = Model.copiesById[id]
241214
}
242215

243-
updateOriginal(copy, current)
216+
_merge(copy, current)
217+
},
244218

245-
// Object.assign(current, copy)
219+
// Removes the copy from copiesById
220+
clearCopy (state, id) {
221+
const newCopiesById = Object.assign({}, state.copiesById)
222+
delete newCopiesById[id]
223+
state.copiesById = newCopiesById
224+
},
225+
226+
clearAll (state) {
227+
state.ids = []
228+
state.currentId = null
229+
state.copy = null
230+
state.keyedById = {}
231+
},
232+
233+
clearList (state) {
234+
let currentId = state.currentId
235+
let current = state.keyedById[currentId]
236+
237+
if (currentId && current) {
238+
state.keyedById = {
239+
[currentId]: current
240+
}
241+
state.ids = [currentId]
242+
} else {
243+
state.keyedById = {}
244+
state.ids = []
245+
}
246246
},
247247

248248
// Stores pagination data on state.pagination based on the query identifier (qid)

src/service-module/state.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export default function makeDefaultState (servicePath, options) {
1818
copiesById: {},
1919
currentId: null,
2020
copy: null,
21-
setCurrentOnGet: true,
22-
setCurrentOnCreate: true,
2321
idField,
2422
servicePath,
2523
autoRemove,
@@ -32,6 +30,9 @@ export default function makeDefaultState (servicePath, options) {
3230
pagination: {},
3331
paramsForServer,
3432
whitelist,
33+
34+
setCurrentOnGet: true,
35+
setCurrentOnCreate: true,
3536

3637
isFindPending: false,
3738
isGetPending: false,

0 commit comments

Comments
 (0)