假设我有 3 个集合Customers
,Contacts
和Messages
Customers {_id, name, address, city, state, zip}
Contacts {_id, customer_id, first_name, last_name, email, phone}
Messages {_id, contact_id, subject, body}
好的,现在我在每个集合上设置了一些属性和方法,以将相关集合作为一个函数引入,该函数可以通过转换直接在文档实例上调用,从而使我能够像{{#each contact}}{{customer.name}}{{/each}}
这里一样在模板中进行菊花链这就是我如何改造他们。
Contact.prototype = {
constructor: Contact,
customer: function () {
return Customers.findOne({_id: this.customer_id});
},
fullName: function () {
return this.first_name + " " + this.last_name;
},
neverContacted: function () {
if (!Messages.findOne({contact_id: this._id})) {
return true;
} else {
return false;
};
}
};
和
Customer.prototype = {
constructor: Customer,
owner: function () {
user = Meteor.users.findOne({_id: this.user_id});
return user.username || user.emails[0].address;
},
contacts: function () {
contacts = Contacts.find({customer_id: this._id}).fetch();
return contacts;
}
};
我的问题是如何根据客户集合的虚拟属性对客户集合进行查询
喜欢customers.find().contacts().neverContacted()
有点像菊花链的主动记录风格?