Last active
August 29, 2015 14:04
-
-
Save miry/532a3a1e9998fd068377 to your computer and use it in GitHub Desktop.
Example how to use jQuery FormatCurrency plugin Accounting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# - Download the jquery plugin from https://github.com/openexchangerates/accounting.js | |
# - Save the min file in the `vendor/assets/javascripts/ folder | |
# - Add this file to `app/assets/priceFormatter.js.coffee` | |
# - Add to `app/assets/application.js.coffee`: | |
# `#= require accounting.js` | |
# `#= require priceFormatter.js` | |
class PriceFormatter | |
constructor: ($scope) -> | |
$scope ||= $('[data-behavior~=price-formatted]') | |
@elements = $scope | |
@bindings() | |
bindings: () -> | |
@elements.live 'focus blur paste change', ()-> | |
# do not want comma in cases like 1,234.89 | |
# do not want $ to be appearing like $23.78 | |
result = accounting.formatMoney($(this).val(), {thousand: '', symbol: ''}) | |
$(this).val(result) | |
#clear on focus 0.00 to allow user enter number without using backspace | |
@elements.live 'focus', ()-> | |
if(parseFloat($(this).val()) == 0) | |
$(this).val("") | |
#revert to 0.00 if blur | |
@elements.live 'blur', ()-> | |
if(parseFloat($(this).val()) == 0) | |
$(this).val("0.00") | |
@elements.trigger('change') | |
$ -> | |
new PriceFormatted() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment