I have entities that have many associations to others and store their ID. I generally need to access the entity by the IDs of the related entities rather than the ID of the entity itself, so I've been trying to use a static property to maintain a cache rather than querying every time.
I have several types of the entity, so I have a parent class that I inherit from and would like to have each subclass override the methods that actually update the cache (since this is dependent on the subclass), but the lifecycle hooks required for Vuex ORM don't really need to change.
The problem I ran into was that this was undefined when the lifecycle hooks were called. Is this the desired behaviour or should this be bound to the Model class for the static lifecycle hook methods? I worked around it by using Object.getPrototypeOf(model) and manually binding the model class.
Desired structure:
import {Model} from '@vuex-orm/core';
export default class BaseModel extends Model {
static mapping = {};
static fields() {
return {
relatedId: this.number(null),
anotherRelatedId: this.number(null),
};
}
static afterCreate(model) {
// I expected `this` to refer to the (sub)class here
return this.updateMap(model);
}
// and other relevant lifecycle hooks here...
// These two methods would be overridden
static updateMap(model) {
return null;
}
static deleteMapEntry(model) {
return null;
}
}
I have entities that have many associations to others and store their ID. I generally need to access the entity by the IDs of the related entities rather than the ID of the entity itself, so I've been trying to use a static property to maintain a cache rather than querying every time.
I have several types of the entity, so I have a parent class that I inherit from and would like to have each subclass override the methods that actually update the cache (since this is dependent on the subclass), but the lifecycle hooks required for Vuex ORM don't really need to change.
The problem I ran into was that
thiswas undefined when the lifecycle hooks were called. Is this the desired behaviour or shouldthisbe bound to the Model class for the static lifecycle hook methods? I worked around it by usingObject.getPrototypeOf(model)and manually binding the model class.Desired structure: