我想要一些我的Meteor系列模型。我能找到的两个选项是使用集合助手:
Users = new Mongo.Collection('users')
Users.helpers({
fullName() {
return `${this.firstName} ${this.lastName}`
}
})
或将返回的对象映射到类实例:
class UserCollection extends Mongo.Collection {
fetch() : User[] {
return super.fetch().map(user => new User(user))
}
}
let users = new UserCollection('users')
class User {
collection: Mongo.Collection
_id: string
firstName: string
lastName: string
constructor({firstName: string, lastName: string) {
this.firstName = firstName
this.lastName = lastName
this.collection = users
}
get fullname () {
return `${this.firstName} ${this.lastName}`
}
save () {
if(this._id) {
this.collection.update({ $set: { ...this }})
}
else {
this.collection.insert({...this})
}
}
}
哪种变体更可取?我更喜欢B,但很少发现任何使用它的例子。