Próbuję wymyślić ładny sposób na zawijanie każdego użytkownika, który pobieram z Kolekcji Konta Meteorów w funkcji, w tym kilka prototypowych funkcji pomocniczych i liczników z innych kolekcji itp. Najlepszym sposobem na opisanie tego jest kod.Czy istnieje dobry sposób na zawijanie każdego Meteor.user w obiekcie z funkcjami prototypowymi?
Funkcja User Chcę owinąć każdy użytkownik w wyglądałby mniej więcej tak:
// - - - - - -
// 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);
}
};
mogę łatwo mieć zmienną globalną „CurrentUser”, który posiada informacje o aktualnie zalogowany użytkownik po stronie klienta jak ten:
Meteor.autorun(function() {
if (Meteor.user()) {
currentUser = new User(Meteor.user().profile.facebook.id);
}
});
to także łatwe do wdrożenia tego w pomocnika kierownicy, zastępując th Zastosowanie e od {{currentUser}}
tak:
Handlebars.registerHelper('thisUser', function() {
if (Meteor.user()) {
return new User(Meteor.user());
} else {
return false;
}
});
Co chcę zrobić oprócz tego jest aby tak, że kiedy wraca Meteor.user Meteor() lub Meteor.users.find ({}) sprowadzić.(), zawiera on funkcje pomocnicze i krótkie uchwyty dla first_name, last_name, itp.
Czy mogę w jakiś sposób rozszerzyć Meteor.user() lub jest jakiś sposób to zrobić?
myślę, że może być brakuje '' 'return''' w tej funkcji _tranform. – Diogenes
To wydaje się działać, ale myślę, że może to później doprowadzić do kłopotów. Zobacz github.com/meteor/meteor/issues/810. Wewnętrzny kod Meteora zakłada, że użytkownicy NIE są przekształcani. – Diogenes
Tak, dziękuję. Dodałem "powrót". – georgedyer