2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* The `Model` class represents a local data resource. It provides methods to
|
|
|
|
* persist changes via the API.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
export default class Model {
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* @param {Object} data A resource object from the API.
|
|
|
|
* @param {Store} store The data store that this model should be persisted to.
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
constructor(data = {}, store) {
|
|
|
|
/**
|
|
|
|
* The resource object from the API.
|
|
|
|
*
|
|
|
|
* @type {Object}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
this.data = data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The time at which the model's data was last updated. Watching the value
|
|
|
|
* of this property is a fast way to retain/cache a subtree if data hasn't
|
|
|
|
* changed.
|
|
|
|
*
|
|
|
|
* @type {Date}
|
|
|
|
* @public
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
this.freshness = new Date();
|
2015-07-15 12:30:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the resource exists on the server.
|
|
|
|
*
|
|
|
|
* @type {Boolean}
|
|
|
|
* @public
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
this.exists = false;
|
2015-07-15 12:30:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The data store that this resource should be persisted to.
|
|
|
|
*
|
|
|
|
* @type {Store}
|
|
|
|
* @protected
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
this.store = store;
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Get the model's ID.
|
|
|
|
*
|
|
|
|
* @return {Integer}
|
|
|
|
* @public
|
|
|
|
* @final
|
|
|
|
*/
|
2015-07-04 10:54:48 +08:00
|
|
|
id() {
|
2015-07-15 12:30:11 +08:00
|
|
|
return this.data.id;
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Get one of the model's attributes.
|
|
|
|
*
|
|
|
|
* @param {String} attribute
|
|
|
|
* @return {*}
|
|
|
|
* @public
|
|
|
|
* @final
|
|
|
|
*/
|
2015-07-07 13:59:21 +08:00
|
|
|
attribute(attribute) {
|
2015-07-15 12:30:11 +08:00
|
|
|
return this.data.attributes[attribute];
|
2015-07-07 13:59:21 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Merge new data into this model locally.
|
|
|
|
*
|
|
|
|
* @param {Object} data A resource object to merge into this model
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
pushData(data) {
|
|
|
|
// Since most of the top-level items in a resource object are objects
|
|
|
|
// (e.g. relationships, attributes), we'll need to check and perform the
|
|
|
|
// merge at the second level if that's the case.
|
|
|
|
for (const key in data) {
|
|
|
|
if (typeof data[key] === 'object') {
|
|
|
|
this.data[key] = this.data[key] || {};
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
// For every item in a second-level object, we want to check if we've
|
|
|
|
// been handed a Model instance. If so, we will convert it to a
|
|
|
|
// relationship data object.
|
|
|
|
for (const deepKey in data[key]) {
|
|
|
|
if (data[key][deepKey] instanceof Model) {
|
|
|
|
data[key][deepKey] = {data: Model.getRelationshipData(data[key][deepKey])};
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
2015-07-15 12:30:11 +08:00
|
|
|
this.data[key][deepKey] = data[key][deepKey];
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
2015-04-25 20:58:39 +08:00
|
|
|
} else {
|
2015-07-15 12:30:11 +08:00
|
|
|
this.data[key] = data[key];
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
// Now that we've updated the data, we can say that the model is fresh.
|
|
|
|
// This is an easy way to invalidate retained subtrees etc.
|
2015-04-25 20:58:39 +08:00
|
|
|
this.freshness = new Date();
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Merge new attributes into this model locally.
|
|
|
|
*
|
|
|
|
* @param {Object} attributes The attributes to merge.
|
|
|
|
* @public
|
|
|
|
*/
|
2015-07-04 10:54:48 +08:00
|
|
|
pushAttributes(attributes) {
|
2015-07-15 12:30:11 +08:00
|
|
|
this.pushData({attributes});
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Merge new attributes into this model, both locally and with persistence.
|
|
|
|
*
|
|
|
|
* @param {Object} attributes The attributes to save. If a 'relationships' key
|
|
|
|
* exists, it will be extracted and relationships will also be saved.
|
|
|
|
* @return {Promise}
|
|
|
|
* @public
|
|
|
|
*/
|
2015-07-04 10:54:48 +08:00
|
|
|
save(attributes) {
|
2015-07-15 12:30:11 +08:00
|
|
|
const data = {
|
|
|
|
type: this.data.type,
|
|
|
|
id: this.data.id,
|
2015-07-04 10:54:48 +08:00
|
|
|
attributes
|
|
|
|
};
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
// If a 'relationships' key exists, extract it from the attributes hash and
|
|
|
|
// set it on the top-level data object instead. We will be sending this data
|
|
|
|
// object to the API for persistence.
|
2015-07-04 10:54:48 +08:00
|
|
|
if (attributes.relationships) {
|
|
|
|
data.relationships = {};
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
for (const key in attributes.relationships) {
|
|
|
|
const model = attributes.relationships[key];
|
|
|
|
|
|
|
|
data.relationships[key] = {
|
|
|
|
data: model instanceof Array
|
|
|
|
? model.map(Model.getRelationshipData)
|
|
|
|
: Model.getRelationshipData(model)
|
2015-06-12 15:11:13 +08:00
|
|
|
};
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
2015-07-04 10:54:48 +08:00
|
|
|
|
|
|
|
delete attributes.relationships;
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
// Before we update the model's data, we should make a copy of the model's
|
|
|
|
// old data so that we can revert back to it if something goes awry during
|
|
|
|
// persistence.
|
|
|
|
const oldData = JSON.parse(JSON.stringify(this.data));
|
2015-05-27 14:55:44 +08:00
|
|
|
|
2015-04-25 20:58:39 +08:00
|
|
|
this.pushData(data);
|
|
|
|
|
2015-05-26 10:13:20 +08:00
|
|
|
return app.request({
|
2015-07-04 10:54:48 +08:00
|
|
|
method: this.exists ? 'PATCH' : 'POST',
|
2015-07-29 19:30:09 +08:00
|
|
|
url: app.forum.attribute('apiUrl') + this.apiEndpoint(),
|
2015-07-15 12:30:11 +08:00
|
|
|
data: {data}
|
|
|
|
}).then(
|
|
|
|
// If everything went well, we'll make sure the store knows that this
|
|
|
|
// model exists now (if it didn't already), and we'll push the data that
|
|
|
|
// the API returned into the store.
|
|
|
|
payload => {
|
|
|
|
this.store.data[payload.data.type][payload.data.id] = this;
|
|
|
|
return this.store.pushPayload(payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
// If something went wrong, though... good thing we backed up our model's
|
|
|
|
// old data! We'll revert to that and let others handle the error.
|
|
|
|
response => {
|
|
|
|
this.pushData(oldData);
|
|
|
|
throw response;
|
|
|
|
}
|
|
|
|
);
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Send a request to delete the resource.
|
|
|
|
*
|
|
|
|
* @param {Object} data Data to send along with the DELETE request.
|
|
|
|
* @return {Promise}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
delete(data) {
|
|
|
|
if (!this.exists) return m.deferred.resolve().promise;
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-05-26 10:13:20 +08:00
|
|
|
return app.request({
|
2015-04-25 20:58:39 +08:00
|
|
|
method: 'DELETE',
|
2015-07-29 19:30:09 +08:00
|
|
|
url: app.forum.attribute('apiUrl') + this.apiEndpoint(),
|
2015-07-15 12:30:11 +08:00
|
|
|
data
|
|
|
|
}).then(
|
|
|
|
() => this.exists = false
|
|
|
|
);
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:30:09 +08:00
|
|
|
/**
|
|
|
|
* Construct a path to the API endpoint for this resource.
|
|
|
|
*
|
|
|
|
* @return {String}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
apiEndpoint() {
|
|
|
|
return '/' + this.data.type + (this.exists ? '/' + this.data.id : '');
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Generate a function which returns the value of the given attribute.
|
|
|
|
*
|
|
|
|
* @param {String} name
|
|
|
|
* @param {function} [transform] A function to transform the attribute value
|
|
|
|
* @return {*}
|
|
|
|
* @public
|
|
|
|
*/
|
2015-07-04 10:54:48 +08:00
|
|
|
static attribute(name, transform) {
|
2015-04-25 20:58:39 +08:00
|
|
|
return function() {
|
2015-07-15 12:30:11 +08:00
|
|
|
const value = this.data.attributes[name];
|
|
|
|
|
|
|
|
return transform ? transform(value) : value;
|
|
|
|
};
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Generate a function which returns the value of the given has-one
|
|
|
|
* relationship.
|
|
|
|
*
|
|
|
|
* @param {String} name
|
|
|
|
* @return {Model|Boolean|undefined} false if no information about the
|
|
|
|
* relationship exists; undefined if the relationship exists but the model
|
|
|
|
* has not been loaded; or the model if it has been loaded.
|
|
|
|
* @public
|
|
|
|
*/
|
2015-07-04 10:54:48 +08:00
|
|
|
static hasOne(name) {
|
2015-04-25 20:58:39 +08:00
|
|
|
return function() {
|
2015-07-15 12:30:11 +08:00
|
|
|
if (this.data.relationships) {
|
|
|
|
const relationship = this.data.relationships[name];
|
|
|
|
|
2015-07-28 16:58:31 +08:00
|
|
|
if (relationship) {
|
|
|
|
return app.store.getById(relationship.data.type, relationship.data.id);
|
|
|
|
}
|
2015-05-30 12:28:21 +08:00
|
|
|
}
|
2015-07-28 15:45:09 +08:00
|
|
|
|
|
|
|
return false;
|
2015-07-15 12:30:11 +08:00
|
|
|
};
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Generate a function which returns the value of the given has-many
|
|
|
|
* relationship.
|
|
|
|
*
|
|
|
|
* @param {String} name
|
|
|
|
* @return {Array|Boolean} false if no information about the relationship
|
|
|
|
* exists; an array if it does, containing models if they have been
|
|
|
|
* loaded, and undefined for those that have not.
|
|
|
|
* @public
|
|
|
|
*/
|
2015-07-04 10:54:48 +08:00
|
|
|
static hasMany(name) {
|
2015-04-25 20:58:39 +08:00
|
|
|
return function() {
|
2015-07-15 12:30:11 +08:00
|
|
|
if (this.data.relationships) {
|
|
|
|
const relationship = this.data.relationships[name];
|
|
|
|
|
2015-07-28 16:58:31 +08:00
|
|
|
if (relationship) {
|
|
|
|
return relationship.data.map(data => app.store.getById(data.type, data.id));
|
|
|
|
}
|
2015-05-30 12:28:21 +08:00
|
|
|
}
|
2015-07-28 15:45:09 +08:00
|
|
|
|
|
|
|
return false;
|
2015-07-15 12:30:11 +08:00
|
|
|
};
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Transform the given value into a Date object.
|
|
|
|
*
|
|
|
|
* @param {String} value
|
|
|
|
* @return {Date|null}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
static transformDate(value) {
|
|
|
|
return value ? new Date(value) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a relationship data object for the given model.
|
|
|
|
*
|
|
|
|
* @param {Model} model
|
|
|
|
* @return {Object}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
static getRelationshipData(model) {
|
|
|
|
return {
|
|
|
|
type: model.data.type,
|
|
|
|
id: model.data.id
|
|
|
|
};
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
}
|