Skip to content

Options.method ambiguous and dangerous #18

@jonrmitchell

Description

@jonrmitchell

Right now the $.fn.mustache function allows you to pass in the name of the jquery method you want it to call. It defaults to append. In my particular use case I wanted it to replace rather than append. The resultant call looked like this:

$('#users').mustache('users_template', data, {method:'html'});

A couple reasons why this call rubs me the wrong way:

  1. I had to go read the source code to know what I needed to do
  2. You open yourself up to problems if you just let the user pass in method names like that. If they pass in the wrong name, they'll get either errors if jquery doesn't have that method, or (probably even worse) unknown if jquery does have that method but it's not applicable.
  3. It's extremely confusing for those that come after me that will need to maintain it, due to the fact that it says 'html'. In most contexts, html is a content type, not a function. Which means they'll have to dig into the source code too in order to figure out what that means.

Here's my suggestion: Instead of specifying the method, specify a mode, with valid values append or replace. Then the plugin can select the jquery method appropriate for the situation. It's clear (both to current and successive users), your users no longer need to know the plugin internals, and it's more error-proof.

Just a suggestion, do with it what you will. I won't be offended either way.

Although at the very least, since there are only a handful of jquery functions that would even make sense to be passed in, (append, html, and val are the only ones I could think of off the top of my head) I'd suggest white-listing the methods that could be used and falling back on your default if they pass in a bogus one.

P.S. Also, default method/mode should be a global setting, so I don't have to change it in the source code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions