2

I'm trying to setup a PromiseProxy Service that returns an Ember Data model, but the result doesn't seem to set the content property.

My service looks like this:

import Ember from 'ember';

const { computed, inject, ObjectProxy, PromiseProxyMixin } = Ember;

export default ObjectProxy.extend(PromiseProxyMixin, {
  isServiceFactory: true,
  store: inject.service(),

  promise: computed({
    get() {
      var store = this.get('store');

      return store.findRecord('community', window.community.id);
    }
  })
});

I then inject this service into the following locations:

export function initialize(container, application) {
  application.inject('controller', 'community', 'service:community');
  application.inject('route', 'community', 'service:community');
  application.inject('model', 'community', 'service:community');
  application.inject('component', 'community', 'service:community');
}

export default {
  name: 'community',
  after: 'store',
  initialize: initialize
};

And then I use it as a model in my application route as a sort of deferReadiness workaround, since my whole app depends on this one model which is used throughout and expected to be there.

export default Ember.Route.extend({
  model() {
    return this.get('community');
  }
});

The issue is that it goes on to other routes, and properties on the community object are not there, i.e. content isn't set. Also community.isPending is true. The CP does get hit and the data comes back (I tested with a then in the CP).

Here is a full gist example: https://gist.github.com/knownasilya/8c9f78d910ed50ec8d84


Edit

So I found a workaround:

  promise: computed({
    get() {
      var store = this.get('store');

      return store.findRecord('community', window.community.id)
        .then(data => {
          this.set('content', data);
          return data;
        })
    }
  })

Seems like it doesn't set the content because model is proxied already?

knownasilya
  • 5,998
  • 4
  • 36
  • 59

1 Answers1

1

Ember Data already wraps its objects in an ObjectProxy, you could just set the object as your service.

Additionally, this syntax is deprecated in future versions syntax for initializers, since it's moved to instance initializers, but no big deal.

 initialize: function (container, application) {
    // the store will be available from the container, 
    // and the name of the store changes depending on which version you are using.
    var store = container.lookup('service:store'),
        community= store.find('community', id);
    application.register("service:community", community, { instantiate: false });
    application.inject("controller", "community", "service:community");
    application.inject("route", "community", "service:community");
    application.inject("component", "community", "service:community");
}

And then you can still return community from the model, beforeModel hook etc.

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • This is what I have currently, but it requires `deferReadiness` since I need the resolved model in other parts of the app. I'm trying to move away from `deferReadiness` and want to move to 2.x so instance-initializers are a must. – knownasilya Mar 10 '16 at 13:44