jaridmargolin / formatter.js

Format html inputs to match a specified pattern
http://firstopinion.github.io/formatter.js
2.48k stars 235 forks source link

Make Formatter usable as an AMD module #14

Closed bryanjswift closed 11 years ago

bryanjswift commented 11 years ago

Defines Formatter as an AMD module if an AMD compatible define function is found in scope. Does the same for CommonJS and falls back to defining it as window['Formatter']. Tests all still pass.

jaridmargolin commented 11 years ago

Overall I like this. I do have one question regarding the following line (and feel free to convince me otherwise)...

if (typeof module !== 'undefined' && module.exports) { module.exports = definition(); }

Is this line necessary? formatter.js runs strictly in a browser. Is there any advantage to exposing the definition via module.exports?

Thanks for the PR

bryanjswift commented 11 years ago

I suppose that depends on what you call necessary. Just because formatter.js is runs in the browser does not mean people don't package their JS using the CommonJS modules style. I don't personally, I use RequireJS, but I do recall reading it is possible just as it is possible to use AMD style modules in Node.

jaridmargolin commented 11 years ago

I only have experience with AMD in the browser, and at the time of my comment, I was unable to find a CommonJS implementation. However, it does look like there are several (taken from http://addyosmani.com/writing-modular-js/):

I should offer the most flexibility possible. Merging. Thanks!

bryanjswift commented 11 years ago

My pleasure. I should have provided examples of CommonJS implementations in my previous comment; apologies.

kenmorechalfant commented 8 years ago

What's the correct way to use Formatter with jQuery in RequireJS?

For example, Backbone has Underscore as a dependency, and optional jQuery integration. I just do: require('jquery','underscore','backbone') ... and everything seems to work.

Now, I tried doing adding the jQuery version of formatter to my paths config and add it to my require statement but I get an error: Uncaught Error: Script error for "jQuery", needed by: formatter

Do you have to "shim" it as I've seen some people do around the net or is there a better way?