Is there a good way to wrap each Meteor.user object in an object using prototypes, etc.?

I am trying to come up with a good way to wrap every user that I get from the Meteor Accounts collection into functions, including several helper functions of the prototype and the number of samples from other collections, etc. The best way to describe this is in code.

The user function that I want to wrap for each user will look something like this:

// - - - - - -
// 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);
    }

};

I can easily have the global variable currentUser, which stores this information about the current user login on the client side as follows:

Meteor.autorun(function() {
    if (Meteor.user()) {
        currentUser = new User(Meteor.user().profile.facebook.id);
    }
});

It is also easy to implement this in the Handlebars helper, replacing the usage {{currentUser}}as follows:

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() ?

+5
2

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); }
});

( , , "" )

+2

:

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

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

UniUsers - Meteor.users

0

All Articles