我正在尝试检查Model是否包含在模型集合中,但即使集合包含我的搜索模型,包含检查仍然失败(myFriendshipStatus):
var attr = DS.attr;
GambifyApp.User = DS.Model.extend({
firstName: attr(),
lastName: attr(),
nickName: attr(),
image: attr(),
friendsWithMe: DS.hasMany('user', {
inverse: 'myFriends', async: true
}),
myFriends: DS.hasMany('user',{
inverse: 'friendsWithMe', async: true
}),
groups: DS.hasMany('group', {async: true}),
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName'),
// Ember.computed.intersect('myFriends', 'friendsWithMe')
friends: function() {
var ret = [];
Ember.RSVP.all([this.get('myFriends'), this.get('friendsWithMe')]).then(function(results) {
ret.pushObjects(_.intersection(results[0].get('content'), results[1].get('content'))) ;
});
return ret;
}.property('myFriends', 'friendsWithMe'),
/**
* Gives the relation between two User
* 4: has requested your friendship
* 3: Yourself
* 2: Friends
* 1: FriendShip Request
*/
myFriendshipStatus: function() {
if(this.get('friends').contains(GambifyApp.User.Current)){
return 2
} else if(this.get('friendsWithMe').contains(GambifyApp.User.Current)){
return 4
} else if(this.get('myFriends').contains(GambifyApp.User.Current)){
return 1
} else if (this.get('id') === GambifyApp.User.Current.id){
return 3
} else {
return 0
}
}.property('myFriends', 'friendsWithMe')
});
这些是我正在使用的灯具:
GambifyApp.User.FIXTURES = [
{ id: 1, firstName: 'Marc', lastName: 'Höffl', nickName: 'marc',
image: 'images/fixtures/Profile/marc.jpg',
friendsWithMe: [2, 3],
myFriends: [2, 3],
groups: [1, 2]
},
{ id: 2, firstName: 'Tobias' , lastName: 'Degele', nickName: 'degi',
image: 'images/fixtures/Profile/degi.jpg',
friendsWithMe: [1, 3, 4, 5],
myFriends: [1, 3],
groups: [1, 2]
},
{ id: 3, firstName: 'Franz' , lastName: 'Herbert', nickName: 'Hubi',
friendsWithMe: [4],
myFriends: [1,2, 3,4,5],
image: 'images/fixtures/Profile/MOF.jpg',
groups: [1]
},
{ id: 4, firstName: 'Christian' , lastName: 'Ditter', nickName: 'Dyder',
friendsWithMe: [3, 5],
myFriends: [2, 3, 5],
image: 'images/fixtures/Profile/user.png',
groups: [ 2]
},
{ id: 5, firstName: 'Strego' , lastName: 'Pengo', nickName: '!',
friendsWithMe: [3, 5],
myFriends: [2, 4, 5],
image: 'images/fixtures/Profile/diggity.jpg',
groups: [ 2]
}
];
这是我当前登录用户的Fixture:
GambifyApp.User.Current = GambifyApp.User.FIXTURES[0]
我已经调试了contains函数,我发现搜索到的模型包含在我的集合中但仍然找不到。
更新 我现在已经解决了它,不是通过全局解决方案,而是通过在所有控制器中注入用户:
Ember.Application.initializer({
name: "currentUser",
initialize: function(container, application) {
var store = container.lookup('store:main');
container.optionsForType('user', { instantiate: false, singleton: true });
container.register('user:current', store.find('user', 1));
}
});
Ember.Application.initializer({
name: "injectCurrentUser",
after: 'currentUser',
initialize: function(container) {
container.injection('controller:application', 'currentUser', 'user:current');
container.typeInjection('route', 'currentUser', 'user:current');
}
});
答案 0 :(得分:1)
包含告诉您确切的对象是否在那里。 Ember Data使用DS.Model包装夹具数据,并且所有这些都是额外的魔法(脏检查,保存等)。所以POJO不会被发现。
因此,您最好找到记录(使用商店)并分配
GambifyApp.User.Current = this.store.find('user', 1);