mirror of
https://github.com/flarum/framework.git
synced 2025-05-22 14:49:57 +08:00
Replace Ember app with Mithril app
This commit is contained in:
67
js/lib/store.js
Normal file
67
js/lib/store.js
Normal file
@ -0,0 +1,67 @@
|
||||
export default class Store {
|
||||
constructor() {
|
||||
this.data = {}
|
||||
this.models = {}
|
||||
}
|
||||
|
||||
pushPayload(payload) {
|
||||
payload.included && payload.included.map(this.pushObject.bind(this))
|
||||
var result = payload.data instanceof Array ? payload.data.map(this.pushObject.bind(this)) : this.pushObject(payload.data);
|
||||
result.meta = payload.meta;
|
||||
result.payload = payload;
|
||||
return result;
|
||||
}
|
||||
|
||||
pushObject(data) {
|
||||
if (!this.models[data.type]) { return; }
|
||||
var type = this.data[data.type] = this.data[data.type] || {};
|
||||
|
||||
if (type[data.id]) {
|
||||
type[data.id].pushData(data);
|
||||
} else {
|
||||
type[data.id] = this.createRecord(data.type, data);
|
||||
}
|
||||
type[data.id].exists = true;
|
||||
return type[data.id];
|
||||
}
|
||||
|
||||
find(type, id, query) {
|
||||
var endpoint = type
|
||||
var params = {}
|
||||
if (id instanceof Array) {
|
||||
endpoint += '?ids[]='+id.join('&ids[]=');
|
||||
params = query
|
||||
} else if (typeof id === 'object') {
|
||||
params = id
|
||||
} else if (id) {
|
||||
endpoint += '/'+id
|
||||
params = query
|
||||
}
|
||||
return m.request({
|
||||
method: 'GET',
|
||||
url: app.config.apiURL+'/'+endpoint,
|
||||
data: params,
|
||||
background: true,
|
||||
config: app.session.authorize.bind(app.session)
|
||||
}).then(this.pushPayload.bind(this));
|
||||
}
|
||||
|
||||
getById(type, id) {
|
||||
return this.data[type] && this.data[type][id];
|
||||
}
|
||||
|
||||
all(type) {
|
||||
return this.data[type] || {};
|
||||
}
|
||||
|
||||
model(type, Model) {
|
||||
this.models[type] = Model;
|
||||
}
|
||||
|
||||
createRecord(type, data) {
|
||||
data = data || {};
|
||||
data.type = data.type || type;
|
||||
|
||||
return new (this.models[type])(data, this);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user