dimanche 2 octobre 2016

Deep watch arbitriary JSON keys with computed properties in Ember

I have a use case where a Model has a JSON attribute used for arbitrary configurations: configuration: DS.attr().

I have a configurationService (initialized in every component/route/controller/…) with computed properties for easily retrieving this configurations throughout the application

Since the JSON configurations are quite large & have variable depth I can’t have a computed property for every single one.

Unfortunately, being the Service a singleton computed properties don’t detect the changes in the JSON keys (a.k.a. deep watch). I there a way to force deep watching a JSON attribute?

Example: I have the following computed property in my service:

profile: Ember.computed('configuration.profile', function() {
  return this.get('configuration.profile');
});

Considering the configuration object was: {configuration: {profile: {show_birthday: false}}}, if I somehow change show_birthday to true, it won’t be detected

  • Note: I considered http://ift.tt/28Ixgaj but discarded it since it’s too verbose, the issue is that our configuration object can become quite large and deep, dynamic & unpredictable
  • I tried computed.alias as well with no success.

Any hints or alternatives would be appreciated :)




Aucun commentaire:

Enregistrer un commentaire