Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject the currentUser into all other User Models

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?

like image 654
m0c Avatar asked Mar 22 '23 02:03

m0c


1 Answers

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.

like image 122
aceofspades Avatar answered Apr 01 '23 21:04

aceofspades