2
\$\begingroup\$

Is this best practice way to send messages to the user?

Update: This has been changed to a simple function call.

/**
 *Message - Sends message to the .innerHTML of an element
 */
var Message = ( function () 
{
 var messages = 
 {
 name: 'Please enter a valid name',
 email: 'Please enter a valid email',
 email_s: 'Please enter a valid email.',
 pass: 'Please enter password, 6-40 characters',
 url: 'Please enter a valid url',
 title: 'Please enter a valid title',
 tweet: 'Please enter a valid tweet',
 empty: 'Please complete all fields',
 same: 'Please make emails equal',
 taken: 'Sorry, that email is taken',
 validate: 'Please contact <a class="d" href="mailto:[email protected]">support</a> to reset your password',
 };
 var MessageInternal = function (element) 
 {
 this.element = element;
 };
 MessageInternal.prototype.display = function( type ) 
 {
 this.element.innerHTML = messages[ type ];
 };
 return MessageInternal;
} () );
asked Apr 11, 2012 at 22:52
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I don't really understand what you are asking here, but I'll give some general pointers:

  • I find that you have two different variables called Message very confusing.

  • It's not really a good idea to add proprietary properties to DOM elements. You should consider using the HTML5 dataset API instead.

  • It seems wrong to hard code the fade effect.

  • IMHO this seems like a lot of code and unnecessarily over-engineered for displaying a simple message. It reminds me a lot how of the Twitter bootstrap library does things.

answered Apr 13, 2012 at 8:48
\$\endgroup\$
3
  • \$\begingroup\$ - decided to go with a simple function instead of module pattern. \$\endgroup\$ Commented Jun 17, 2012 at 21:44
  • \$\begingroup\$ - how is HTML 5 related here?...are you referring to the HTML5 data- attribute? \$\endgroup\$ Commented Jun 17, 2012 at 21:45
  • \$\begingroup\$ this is what you mean by dataset API methinks \$\endgroup\$ Commented Jun 18, 2012 at 15:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.