我在显示用户列表的页面上使用Backbone.js,木偶.js和Backbone-relational。每个用户被标识为"pending"或"active"。"待定"用户是指已收到加入邀请但尚未接受的用户;"活跃的"是那些已经确认的人。挂起用户列表由一个API调用获取,活动用户列表由另一个API调用获取。
情况是这样的:
将一个用户(我们称他为"Miguel")提取到活动用户集合中。但是,如果将与Miguel具有相同ID的"pending"用户提取到pending用户集合中,Miguel将收到属性"Status":"pending"。我可以验证Miguel没有被提取到挂起用户的集合中(或传递给——的解析函数)。
所以我想知道主干关系是否有问题与两个集合的关系,其模型id有时相互匹配。
游戏介绍:If当前用户的集合:{Id: 22日"FirstName":"Miguel"}
And暂挂用户集合:{Id: 22日"FirstName":"贝蒂","状态":"等待"}
那么 Miguel最终看起来像:{Id: 22日"FirstName":米盖尔,"状态":"等待"}
下面是我的模型和集合的代码:/**
* "NV" is a base class we created for this application. It handles .save(), .toPatchJSON(), tracks changed attrs and other such functions.
*/
/**
* This is the collection of "pending" users
*/
var PendingUsers = NV.Collection.extend({
model: User,
initialize: function(models, options) {
Backbone.Collection.prototype.initialize.call(this,models,options);
this.org = options.org;
},
url: function() {
return "/api/organization/" + this.org.get("Id") + "/invites";
},
parse: function(resp, options) {
// "Miguel" never shows up in this function yet obtains the "Pending" Status property.
return _.map(resp, function(invite) {
return {
Id: invite.Id,
FirstName: invite.FirstName,
LastName: invite.LastName,
Email: invite.Email,
InvitationTime: invite.InvitationTime + "+00:00",
Status: 'Pending'
}
});
}
});
/**
* This is the collection of "active" users
*/
var ActiveUsers = NV.Collection.extend({
model: User,
initialize: function(models, options) {
Backbone.Collection.prototype.initialize.call(this,models,options);
this.org = options.org;
},
url: function() {
return "/api/organization/" + this.org.get("Id") + "/users";
}
});
/**
* The collections of active and pending users are relations of the "Organization" model. This is what's passed to the View that renders the list of users.
*/
var Organization = NV.Model.extend({
defaults: {
Name: ''
},
relations: [
{
type: Backbone.HasMany,
key: 'Users',
relatedModel: User,
collectionType: ActiveUsers,
collectionOptions: function(org) {
return {'org': org };
}
},
{
type: Backbone.HasMany,
key: 'InvitedUsers',
relatedModel: User,
collectionType: PendingUsers,
collectionOptions: function(org) {
return {'org': org };
}
}
],
urlRoot: "/api/organization"
});
return Organization;
/**
* Both pending and active collections build from the User model, so here it is
*/
var User = NV.Model.extend({
schema: {
Name: 'Text',
Email: { validators: ['required', 'email'] },
password: 'Password'
},
defaults: {
FirstName: '',
LastName: '',
Email: ''
},
fetch: function(options) {
this.me = options.me || false;
NV.Model.prototype.fetch.call(this,options);
},
avatar: function(options){
return "/api/user/" + this.get("Id") + "/avatar";
},
url: function() {
if (this.me) return "/api/user/me";
return "/api/user/" + this.get("Id");
}
});
return User;
如果我没有包括重要的信息,请告诉我。在这个问题上!和谢谢!
你说得对,Backbone。关系型公司对此有问题。在幕后,它保留了给定类型的所有模型的全局集合——在这里是所有用户。取回时发生的是Backbone行为。它识别出您正在进入现有的集合(全局集合),找到具有相同ID的模型并尝试合并数据,这将导致"Miguel"模型上的"Status"属性。
这通常是一个理想的功能,因为保持具有相同id的相同类型的多个模型通常表明一些问题或次优数据加载。
一个快速的解决方法是通过扩展User来创建额外的User '类',如PendingUser。