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;
} () );
\$\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.

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

Your Answer

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