是否有一种很好的方法可以将每个Meteor.user包装在一个具有原型函数之类的对象中?

是否有一种很好的方法可以将每个Meteor.user包装在一个具有原型函数之类的对象中?,meteor,Meteor,我正试图找到一种很好的方法,将我从Meteor Accounts集合中获取的每个用户包装到一个函数中,包括一些原型助手函数和其他集合中的计数等。最好的描述方法是用代码 我想包装每个用户的用户函数如下所示: // - - - - - - // USER OBJECT // - - - - - - var currentUser = null; // holds the currentUser object when aplicable function User(fbId) { var

我正试图找到一种很好的方法,将我从Meteor Accounts集合中获取的每个用户包装到一个函数中,包括一些原型助手函数和其他集合中的计数等。最好的描述方法是用代码

我想包装每个用户的用户函数如下所示:

// - - - - - -
// USER OBJECT
// - - - - - -

var currentUser = null; // holds the currentUser object when aplicable

function User(fbId) {
    var self   = this,
        u      = (typeof id_or_obj == 'string' || id_or_obj instanceof String ? Meteor.users.findOne({'profile.facebook.id': id_or_obj}) : id_or_obj);

    self.fb_id = parseInt(u.profile.facebook.id, 10),

    // Basic info
    self.first_name = u.profile.facebook.first_name,
    self.last_name  = u.profile.facebook.last_name,
    self.name       = u.name,
    self.birthday   = u.birthday,
    self.email      = u.profile.facebook.email,

    // Quotes
    self.likeCount  = Likes.find({fb_id: self.fb_id}).count() || 0;
}

// - - - - - - -
// USER FUNCTIONS
// - - - - - - -

User.prototype = {

    // Get users avatar
    getAvatar: function() {
        return '//graph.facebook.com/' + this.fb_id + '/picture';
    },

    getName: function(first_only) {
        return (first_only ? this.first_name : this.name);
    }

};
Meteor.autorun(function() {
    if (Meteor.user()) {
        currentUser = new User(Meteor.user().profile.facebook.id);
    }
});
Handlebars.registerHelper('thisUser', function() {
    if (Meteor.user()) {
        return new User(Meteor.user());
    } else {
        return false;
    }
});
我可以很容易地拥有一个全局“currentUser”变量,该变量保存客户端当前登录用户的信息,如下所示:

// - - - - - -
// USER OBJECT
// - - - - - -

var currentUser = null; // holds the currentUser object when aplicable

function User(fbId) {
    var self   = this,
        u      = (typeof id_or_obj == 'string' || id_or_obj instanceof String ? Meteor.users.findOne({'profile.facebook.id': id_or_obj}) : id_or_obj);

    self.fb_id = parseInt(u.profile.facebook.id, 10),

    // Basic info
    self.first_name = u.profile.facebook.first_name,
    self.last_name  = u.profile.facebook.last_name,
    self.name       = u.name,
    self.birthday   = u.birthday,
    self.email      = u.profile.facebook.email,

    // Quotes
    self.likeCount  = Likes.find({fb_id: self.fb_id}).count() || 0;
}

// - - - - - - -
// USER FUNCTIONS
// - - - - - - -

User.prototype = {

    // Get users avatar
    getAvatar: function() {
        return '//graph.facebook.com/' + this.fb_id + '/picture';
    },

    getName: function(first_only) {
        return (first_only ? this.first_name : this.name);
    }

};
Meteor.autorun(function() {
    if (Meteor.user()) {
        currentUser = new User(Meteor.user().profile.facebook.id);
    }
});
Handlebars.registerHelper('thisUser', function() {
    if (Meteor.user()) {
        return new User(Meteor.user());
    } else {
        return false;
    }
});
在Handlebar助手中也很容易实现这一点,取代了
{{currentUser}}
的使用,如下所示:

// - - - - - -
// USER OBJECT
// - - - - - -

var currentUser = null; // holds the currentUser object when aplicable

function User(fbId) {
    var self   = this,
        u      = (typeof id_or_obj == 'string' || id_or_obj instanceof String ? Meteor.users.findOne({'profile.facebook.id': id_or_obj}) : id_or_obj);

    self.fb_id = parseInt(u.profile.facebook.id, 10),

    // Basic info
    self.first_name = u.profile.facebook.first_name,
    self.last_name  = u.profile.facebook.last_name,
    self.name       = u.name,
    self.birthday   = u.birthday,
    self.email      = u.profile.facebook.email,

    // Quotes
    self.likeCount  = Likes.find({fb_id: self.fb_id}).count() || 0;
}

// - - - - - - -
// USER FUNCTIONS
// - - - - - - -

User.prototype = {

    // Get users avatar
    getAvatar: function() {
        return '//graph.facebook.com/' + this.fb_id + '/picture';
    },

    getName: function(first_only) {
        return (first_only ? this.first_name : this.name);
    }

};
Meteor.autorun(function() {
    if (Meteor.user()) {
        currentUser = new User(Meteor.user().profile.facebook.id);
    }
});
Handlebars.registerHelper('thisUser', function() {
    if (Meteor.user()) {
        return new User(Meteor.user());
    } else {
        return false;
    }
});
除此之外,我想做的是使Meteor返回Meteor.user()或Meteor.users.find({}).fetch()时,它包括这些帮助函数和first_name、last_name等的短句柄


我可以扩展Meteor.user()吗?或者有什么方法可以做到这一点吗?

在Meteor 0.5.8中,您可以添加如下转换函数:

Meteor.users._transform = function(user) { 
  // attach methods, instantiate a user class, etc.
  // return the object
  // e.g.: 
  return new User(user);
} 
您可以对非用户集合执行相同的操作,但在实例化集合时也可以这样做:

Activities = new Meteor.Collection("Activities", {
  transform: function (activity) { return new Activity(activity); }
});

(这种方式似乎不适用于“特殊”用户集合)

您可以使用meteor软件包

这样做:

UniUsers.UniUser.prototype = {
    getAvatar: function() {
        return '//graph.facebook.com/' + this.fb_id + '/picture';
    }
};

var user = UniUsers.findOne();
console.log(user.getAvatar());

UniUsers是Meteor的扩展集合。users

我认为您可能在该转换函数中缺少了一个
return
。这似乎可行,但我认为可能会导致以后的麻烦。参见github.com/meteor/meteor/issues/810。内部Meteor代码假定用户未被转换。