Best way to add attributes dynamically


#1

Hi,

I’m looking for a way to add attributes based on custom inputs. I did something like this in an initializer :

export function initialize(applicationInstance) {
  var store = applicationInstance.lookup('service:store');

  store.findAll('CustomField').then((customFields) => {
    var attributes = {};
    customFields.forEach((customField) => {
      attributes[customField.get('property')] = DS.attr();
    });
    Subscription.reopen(attributes);
  });
} 

It works but asynchrously so if I instanciate an object before the success of the call, it will not have the custom attributes. I can probably do it synchronously but I will have perf problem. It will slow the load of the app.

There may be a way to override the serialization but I don’t know if it’s a good solution because I will use this attributes in the application and they will not be visible. Kind of “Ghost attributes”.

With a relationship, I will have the same problems.

What’s the best solution?

Thanks!