This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/app/models/rest.js
David Taylor 9534f13256
DEV: Replace site-settings:main with service:site-settings (#17734)
This will allow consumers to inject it using `siteSettings: service()` in preparation for the removal of implicit injections in Ember 4.0. `site-settings:main` is still available and will print a deprecation notice.
2022-08-01 09:43:33 +01:00

125 lines
3.1 KiB
JavaScript

import EmberObject from "@ember/object";
import { Promise } from "rsvp";
import { equal } from "@ember/object/computed";
import { getOwner } from "discourse-common/lib/get-owner";
import { warn } from "@ember/debug";
const RestModel = EmberObject.extend({
isNew: equal("__state", "new"),
isCreated: equal("__state", "created"),
isSaving: false,
beforeCreate() {},
afterCreate() {},
beforeUpdate() {},
afterUpdate() {},
update(props) {
if (this.isSaving) {
return Promise.reject();
}
props = props || this.updateProperties();
this.beforeUpdate(props);
this.set("isSaving", true);
return this.store
.update(this.__type, this.id, props)
.then((res) => {
const payload = this.__munge(res.payload || res.responseJson);
if (payload.success === "OK") {
warn("An update call should return the updated attributes", {
id: "discourse.rest-model.update-attributes",
});
res = props;
}
this.setProperties(payload);
this.afterUpdate(res);
res.target = this;
return res;
})
.finally(() => this.set("isSaving", false));
},
_saveNew(props) {
if (this.isSaving) {
return Promise.reject();
}
props = props || this.createProperties();
this.beforeCreate(props);
const adapter = this.store.adapterFor(this.__type);
this.set("isSaving", true);
return adapter
.createRecord(this.store, this.__type, props)
.then((res) => {
if (!res) {
throw new Error("Received no data back from createRecord");
}
// We can get a response back without properties, for example
// when a post is queued.
if (res.payload) {
this.setProperties(this.__munge(res.payload));
this.set("__state", "created");
}
this.afterCreate(res);
res.target = this;
return res;
})
.finally(() => this.set("isSaving", false));
},
createProperties() {
throw new Error(
"You must overwrite `createProperties()` before saving a record"
);
},
save(props) {
return this.isNew ? this._saveNew(props) : this.update(props);
},
destroyRecord() {
return this.store.destroyRecord(this.__type, this);
},
});
RestModel.reopenClass({
// Overwrite and JSON will be passed through here before `create` and `update`
munge(json) {
return json;
},
create(args) {
args = args || {};
let owner = getOwner(this);
// Some Discourse code calls `model.create()` directly without going through the
// store. In that case the injections are not made, so we do them here. Eventually
// we should use the store for everything to fix this.
if (!args.store) {
args.store = owner.lookup("service:store");
}
if (!args.siteSettings) {
args.siteSettings = owner.lookup("service:site-settings");
}
if (!args.appEvents) {
args.appEvents = owner.lookup("service:appEvents");
}
args.__munge = this.munge;
return this._super(this.munge(args, args.store));
},
});
export default RestModel;