Skip to content

Commit 7eb4c81

Browse files
committed
added support for saving with query strings
1 parent 8eec2c0 commit 7eb4c81

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/model.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,12 @@ class Model {
113113
} else {
114114
method = _options.method
115115
}
116-
const path = _options.path
116+
let path = _options.path
117117
? '/' + _options.path
118118
: ''
119+
if (_options.query) {
120+
path += `?${utils.makeQueryString(_options.query)}`
121+
}
119122
const request = this.$request(this.url + path, {
120123
method,
121124
body

src/tests/tests.js

+42
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,17 @@ export default (Vue, Model, User) => {
293293
.toBe('testtesttest')
294294
})
295295
})
296+
297+
it('should save via the custom url function', () => {
298+
expect.assertions(1)
299+
return user.save({
300+
test: true
301+
})
302+
.then(() => {
303+
expect(global.fetch.mock.calls[global.fetchCount - 1][0])
304+
.toBe('testtesttest')
305+
})
306+
})
296307
})
297308

298309
describe('VueModels - custom urls - property', () => {
@@ -308,4 +319,35 @@ export default (Vue, Model, User) => {
308319
})
309320
})
310321
})
322+
323+
describe('VueModels - save with query', () => {
324+
it('should save with a query string', () => {
325+
const user = new User()
326+
expect.assertions(1)
327+
return user.save({ test: true }, {
328+
query: {
329+
save: false
330+
}
331+
})
332+
.then(() => {
333+
expect(global.fetch.mock.calls[global.fetchCount - 1][0])
334+
.toBe('users?save=false')
335+
})
336+
})
337+
338+
it('should save with multiple query strings', () => {
339+
const user = new User()
340+
expect.assertions(1)
341+
return user.save({ test: true }, {
342+
query: {
343+
save: false,
344+
test: true
345+
}
346+
})
347+
.then(() => {
348+
expect(global.fetch.mock.calls[global.fetchCount - 1][0])
349+
.toBe('users?save=false&test=true')
350+
})
351+
})
352+
})
311353
}

src/utils/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,13 @@ export const resetAllStates = (state, modules) => {
224224
resetState(state[key], modules[key].defaults())
225225
})
226226
}
227+
228+
// assemble query string
229+
230+
export const makeQueryString = (obj) => {
231+
const arr = []
232+
for (let key in obj) {
233+
arr.push(`${key}=${obj[key]}`)
234+
}
235+
return arr.join('&')
236+
}

0 commit comments

Comments
 (0)