4

in my application I have several Users that can be friends. Now I am trying to build a function that gets the "friendShipStatus" from the current logged in user to another User. This is a follow up question from this one: Intersection of promises and dependent computed properties

 /**
 *  Gives the relation between two User
 *  4: has requested your friendship
 *  3: Yourself
 *  2: Friends
 *  1: FriendShip Request
 */
friendShipStatus: function() {
        return this.container.lookup('user:current').then(function(user){
            if(this.get('friends').contains(user)){
                return 2;
            } else if(this.get('friendsWithMe').contains(user)){
                return 4;
            } else if(this.get('myFriends').contains(user)){
                return 1;
            } else if (this.get('id') === user.get('id')){
                return 3;
            } else {
                return 0;
            }
        });

}.property('friends.@each')

The Promise stuff is already an attempt, but not working. I would rather prefer to have the currentUser injected and observe then the property, that as soon as the current user is resolved the property changes. My attempt therefore:

Ember.Application.initializer({
  name: "currentUser",

  initialize: function(container, application) {
    var store = container.lookup('store:main');
    container.register('user:current', store.find('user', 1), { instantiate: false, singleton: true });
  }
});

Ember.Application.initializer({
  name: "injectCurrentUser",
  after: 'currentUser',

  initialize: function(container) {
    // container.injection('controller:application', 'currentUser', 'user:current');
    container.typeInjection('route', 'currentUser', 'user:current');
    container.typeInjection('controller', 'currentUser', 'user:current');
    container.typeInjection('model', 'currentUser', 'user:current');
    container.injection('user:model', 'currentUser', 'user:current');
  }
});

I have already tried it with a type injection and a regular injection. But within my usermodel my currentUser property is always undefined.

How can I inject the current User into my User Model?

Community
  • 1
  • 1
m0c
  • 2,180
  • 26
  • 45

3 Answers3

3

You're not waiting for the user to be returned from ember data before registering. You'll probably want to defer readiness to block until it's back. Try something like this:

Ember.Application.initializer({
  name: "currentUser",

  initialize: function(container, application) {
    application.deferReadiness();
    container.lookup('store:main').find('user', 1).then(function(user) {
      application.register('user:current', user, { instantiate: false, singleton: true });
      application.inject('route', 'currentUser', 'user:current');
      application.inject('controller', 'currentUser', 'user:current');
      application.advanceReadiness();
    }, function() {
      application.advanceReadiness();
    });
  }
});

I'm not sure why you'd want to inject into your model, route/controller is sufficient in my case. Also note this is one initializer rather than two.

aceofspades
  • 7,568
  • 1
  • 35
  • 48
  • This is exactly what I was looking for, good stuff. However, it seems that the store is not available when this runs. Do you know if there is a way to defer running the initializer? – Panman Jan 06 '15 at 22:39
  • FYI, I just posted another solution below. – Panman Jan 06 '15 at 23:17
3

Just figured out another solution using ember-cli's services. First generate a service ember g service user. Then inject the store onto the service so you can do the find(). Next change your service from a standard Object to ObjectProxy. Finally on init() do the find and set the result to content. Here is what I have in the end:

// app/initializers/user-service.js
export function initialize(container, application) {
    application.inject('route', 'user', 'service:user');
    application.inject('controller', 'user', 'service:user');
    application.inject('service:user', 'store', 'store:main');
}

export default {
    name: 'user-service',
    initialize: initialize
};

.

// app/services/user.js
import Ember from 'ember';

export default Ember.ObjectProxy.extend({
    init: function(){
        this.set('content', this.store.find('user', 1));
    }
});
Panman
  • 1,157
  • 2
  • 8
  • 19
1

Regarding model injection, it is in fact possible, see the answer here

Injecting dependencies into an Ember model

Community
  • 1
  • 1
technicolorenvy
  • 486
  • 7
  • 16