Closed nicklandgrebe closed 5 years ago
Does anybody know the proper way to define a class in ES6?
Currently I have something like:
const User = (function() {
class _User extends Api.Base {
static initClass() {
this.className = 'User';
this.queryName = 'users';
}
}
_User.initClass();
return _User;
}());
export default User;
Which seems to work partially, but I'm running into issues with some functions like ResourceLibrary.constantize
.
I can't say one way or another if your class is configured properly for ES6.
The one thing I can say is that ResourceLibrary.constantize
requires that any constant like User
be defined on the ResourceLibrary
instance itself.
In your case, that would mean setting Api.User = (function() { class _User ... })
. The key that you use on Api
must match the _User.className
static property
Thanks for the quick response, looks like the correct ES6 implementation was actually:
import Api from './Api';
const _User = (Api.prototype.User = class User extends Api.Base {
static initClass() {
this.className = 'User';
this.queryName = 'users';
}
});
_User.initClass();
export default _User;
Can now define using
ResourceLibrary.createResource(
class User {
static define = function() {
this.hasMany('notifications');
}
}
)
className
and queryName
will be determined based on class name itself, and can be manually overridden in define
Stories
className
,queryName
, and associations.Objectives
hasMany
from being called wheneveres7.classProperties