migrate to vue3; autocomplete still needs work

This commit is contained in:
bkfox
2022-03-11 18:37:57 +01:00
parent ab8858154b
commit 5b788ca28f
34 changed files with 457 additions and 17868 deletions

View File

@ -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)