如何将currentUser注入所有其他用户模型 [英] How to inject the currentUser into all other User Models

查看:78
本文介绍了如何将currentUser注入所有其他用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有几个可以成为朋友的用户。现在我正在尝试构建一个功能,从当前登录的用户获取friendShipStatus到另一个用户。
这是一个后续问题:承诺和依赖计算属性的交集

  / ** 
*给予关系两个用户
* 4:已请求您的友谊
* 3:您自己
* 2:朋友
* 1:FriendShip请求
* /
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')){
返回3;
} else {
return 0;
}
});

} .property('friends。@ each')

承诺东西已经是一个尝试,但不工作。我宁愿更改当前的用户注册,然后观察属性,一旦当前用户解决了属性更改。我的尝试是:

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

我已经尝试了类型注入和定期注入。但是在我的用户模型中,我的currentUser属性总是未定义的。



如何将当前用户注入到我的用户模型中?

解决方案

只是想出了使用ember-cli的服务的另一种解决方案。首先生成一个服务 ember g service user 。然后将存储注入到服务中,以便您可以执行 find()。接下来,将您的服务从标准的对象更改为 ObjectProxy 。最后在 init()中进行查找并将结果设置为内容。这是我最终的结果:

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


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?

解决方案

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

这篇关于如何将currentUser注入所有其他用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆