How do I attach a new method to a collection object in meteorjs?
user = Meteor.user()
user.newMethod() // how/where do I attach this function?
How do I attach a new method to a collection object in meteorjs?
user = Meteor.user()
user.newMethod() // how/where do I attach this function?
The thing is Meteor.user()
returns the result of something like Meteor.findOne({_id: Meteor.userId()});
so you have to redefine the entire method because there's no prototype object.
Client js, Server js or both:
Meteor.startup(function() {
Meteor.user = function() {
var user = Meteor.users.findOne({_id: Meteor.userId()});
if(!user) return null;
_.extend(user, { // Lodash method https://lodash.com/docs/4.17.4#assign
newMethod: function() {
//Do something
},
newMethod1: function() {
//Do something
}
});
return user;
}
});
You can simply do this:
user = Meteor.user();
user.newMethod = function () {
// Do something
};
and later on
user.newMethod();