Skip to content

Commit a0b43fd

Browse files
committed
updated to allow custom url override
1 parent c706e33 commit a0b43fd

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

src/demo/user.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class User extends Model {
1212
const basePath = this.$options.basePath
1313
return basePath || this.role + 's'
1414
} else {
15-
return ''
15+
return 'users'
1616
}
1717
},
1818
full_name() {

src/model.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class Model {
5252
},
5353
urlRoot() {
5454
const url = _options.url
55-
? _options.url
55+
? typeof _options.url === 'function'
56+
? _options.url()
57+
: _options.url
5658
: `${this.basePath}/${this.id}`
5759
return url
5860
},
@@ -61,7 +63,9 @@ class Model {
6163
},
6264
url() {
6365
const url = _options.url
64-
? _options.url
66+
? typeof _options.url === 'function'
67+
? _options.url()
68+
: _options.url
6569
: this.isNew
6670
? this.basePath
6771
: this.urlRoot

src/tests/tests.js

+40
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ export default (Vue, Model, User) => {
2222
})
2323
})
2424

25+
it('should be fetching the url :name/:id', () => {
26+
expect(global.fetch.mock.calls[0][0])
27+
.toBe('users/123')
28+
})
29+
30+
it('should have updated url based on the model data', () => {
31+
expect(model.url)
32+
.toBe('tenants/593af5eae4e0554e76b71f8a')
33+
})
34+
2535
it('should resolve toJSON', () => {
2636
expect(model.toJSON())
2737
.toBeInstanceOf(Object)
@@ -210,4 +220,34 @@ export default (Vue, Model, User) => {
210220
.toBe('')
211221
})
212222
})
223+
224+
describe('VueModels - custom urls - function', () => {
225+
const user = new User(null, {
226+
url() {
227+
return 'testtesttest'
228+
}
229+
})
230+
it('should fetch via the custom url function', () => {
231+
expect.assertions(1)
232+
return user.fetch()
233+
.then(() => {
234+
expect(global.fetch.mock.calls[4][0])
235+
.toBe('testtesttest')
236+
})
237+
})
238+
})
239+
240+
describe('VueModels - custom urls - property', () => {
241+
const user = new User(null, {
242+
url: 'testproperty'
243+
})
244+
it('should fetch via the custom url function', () => {
245+
expect.assertions(1)
246+
return user.fetch()
247+
.then(() => {
248+
expect(global.fetch.mock.calls[5][0])
249+
.toBe('testproperty')
250+
})
251+
})
252+
})
213253
}

static/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)