How to introduce currentUser in all other user models

In my application, I have several users who can be friends. Now I'm trying to create a function that receives "friendShipStatus" from the current logged in user to another user. This is the next question from this: 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')

Promise is already an attempt, but it does not work. I would rather have currentUser enter and observe the property then, that as soon as the current user is allowed, the property will change. Therefore, my attempt:

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 already tried this with type injection and regular injection. But in my usermodel, the currentUser property is always undefined.

How can I insert the current user into my user model?

+4
3

ember . , , . - :

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();
    });
  }
});

, , / . , , .

+3

, ember-cli. ember g service user. store , find(). Object ObjectProxy. , init() find content. :

// 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));
    }
});
+3

As for the injection model, this is actually possible, see the answer here

Including Dependencies in the Ember Model

+1
source

All Articles