Duplicate Meteor Code

I was working on a small application in Meteor. I noticed a code template that starts to scare me.

Template.userForm.helpers({ name: function(){ user = Meteor.users.findOne(Session.get('edit-user')); return user && user.profile.name; }, _user_id: function(){ user = Meteor.users.findOne(Session.get('edit-user')); return user && user._id; }, email: function(){ user = Meteor.users.findOne(Session.get('edit-user')); return user && user.emails && user.emails[0].address; }, }); 

The problem is repeating the variable && variable.attribute code. If I do not write the code this way, I get errors about undefined variables.

Is there a better way to do this? What am I missing?

+4
source share
2 answers

return variable && variable.attribute equivalent to more complex

 if(variable) return variable.attrubite; return false; 

This is necessary because if variable was null - which happens all the time between page loading and collection inflation - calling variable.attribute throws an exception: null has no attributes.

So no, there is no way out of this test. You can choose a different fragrance if it bothers you - personally, I leave the actual return for the last line and check the correctness earlier:

 if(! variable) return null; return variable.attribute; 

What can be avoided is a line that is also repeated in all your helpers:

 user = Meteor.users.findOne(Session.get('edit-user')); 


In the above case, all attributes belong to the same object. So why not pass this single object?

userForm.js:

 Template.userForm.user = function() { return Meteor.users.findOne(Session.get('edit-user')); } 

userForm.html:

 <template name="userForm"> <span data-id="{{user._id}}"> Name: {{user.name}}, email: {{user.email}} </span> </template> 

or even:

 <template name="userForm"> {{#with user}} <span data-id="{{_id}}"> Name: {{name}}, email: {{email}} </span> {{/with}} </template> 
+6
source

You can define a global helper, especially when using Session variables, which will be much more flexible and allow you to use them for multiple templates:

 Handlebars.registerHelper("getUserProperty", function(field) { var user = Meteor.users.findOne(Session.get('edit-user')); return user && user[field]; }); 

Then you can use this with {{getUserProperty "username"}} or something like that. You can also let the field argument be a little more attractive so that you can use something like "profile.name" and index it multiple times. Something like the following, although you could definitely support more parsing logic:

 Handlebars.registerHelper("getUserProperty", function(field) { var item = Meteor.users.findOne(Session.get('edit-user')); var keys = field.split("."); $(keys).each( function(idx, value) { item = item && item[value]; }); return item; }); 

One note: you should probably use var more when declaring local variables. You do not need to do this in Coffeescript, but it is very important in Javascript.

+3
source

Source: https://habr.com/ru/post/1490747/


All Articles