jeudi 19 mai 2016

Observer not firing for special character property Ember.js

I have a service that maintains an object that get's properties added dynamically, and I want to observe changed on those properties.

CacheService = Ember.Service.extend
  cache: Ember.Object.create()

Eventually properties will be added to this cache, but they follow a special naming convention: %2Fitem%2F1234, and each property is also an object containing properties.

I am explicitly creating an observer that will observe one of this properties to verify whether it fires or not.

cache: Ember.inject.service('cache')

cacheObserver: Ember.observer('cache.cache.%2Fitems%2F1234.attachments.[]', ->
  console.log('foo')).on('init')

The property is set with:

setCacheContent = (itemId, relation, content) ->
  arr = @_getCacheContent() # old content accessing the cache object.
  arr.replace 0, arr.length, Ember.copy(content, false)

But the observer is never fired despite of properly adding content with a new relation inside. Content is added to the array like:

  addContent: (item, relation, content) ->
    arr = Ember.copy @_getCacheContent( item, relation ), false
    content.map (item) =>
      arr.pushObject item if not arr.contains item

I have seen that when accessing properties with % the bracket notation is needed instead of dot notation, but I don't know any other way of setting the observer.




Aucun commentaire:

Enregistrer un commentaire