forked from rc/aircox
migrate to vue3; autocomplete still needs work
This commit is contained in:
@ -20,7 +20,7 @@ export function getCsrf() {
|
||||
// TODO: prevent duplicate simple fetch
|
||||
export default class Model {
|
||||
constructor(data, {url=null}={}) {
|
||||
this.url = url;
|
||||
this.url = url || data.url_;
|
||||
this.commit(data);
|
||||
}
|
||||
|
||||
@ -45,14 +45,24 @@ export default class Model {
|
||||
}
|
||||
}
|
||||
|
||||
static fromList(items, args=null) {
|
||||
return items ? items.map(d => new this(d, args)) : []
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch item from server
|
||||
*/
|
||||
static fetch(url, options=null, args=null) {
|
||||
static fetch(url, {many=false, ...options}={}, args={}) {
|
||||
options = this.getOptions(options)
|
||||
return fetch(url, options)
|
||||
.then(response => response.json())
|
||||
.then(data => new this(data, {url: url, ...args}));
|
||||
const request = fetch(url, options).then(response => response.json());
|
||||
if(many)
|
||||
return request.then(data => {
|
||||
if(!(data instanceof Array))
|
||||
data = data.results
|
||||
return this.fromList(data, args)
|
||||
})
|
||||
else
|
||||
return request.then(data => new this(data, {url: url, ...args}));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,7 +133,7 @@ export class Set {
|
||||
* Fetch multiple items from server
|
||||
*/
|
||||
static fetch(model, url, options=null, args=null) {
|
||||
options = this.getOptions(options)
|
||||
options = model.getOptions(options)
|
||||
return fetch(url, options)
|
||||
.then(response => response.json())
|
||||
.then(data => (data instanceof Array ? data : data.results)
|
||||
|
Reference in New Issue
Block a user