nicklandgrebe / active-resource.js

ActiveResource.js - API resource relational mapping in JavaScript
https://active-resource.js.org
MIT License
133 stars 20 forks source link

Raw definitions #13

Closed nicklandgrebe closed 5 years ago

nicklandgrebe commented 7 years ago

Stories

Objectives

ch-tactica commented 6 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.

nicklandgrebe commented 6 years ago

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

ch-tactica commented 6 years ago

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;
nicklandgrebe commented 5 years ago

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