Rails 3.2 アプリ内のいくつかのフォームを AngularJS を使用するように変換して、ライブ計算などを実行できるようにしています。私の Rails アプリでは、money-railsを使用して通貨を処理しています。これは、すべての通貨フィールドをセントで構成される整数として扱います。
これは、すべての情報を JSON 経由で AngularJS テンプレートに送信するときに問題になります。今では、フォームをドルとセントで表示したいときに、すべてセントで表示されています。
AngularJSコントローラーに変換を入れたので、サーバーからデータを取得すると、更新する直前にセントからドルとセントに、またはその逆に変換します。コードは次のとおりです。
# Edit Investor
window.InvestorEditCtrl = ($scope, $routeParams, $location, Investor, Common) ->
console.log 'InvestorEditCtrl'
# Setup variable for common services(factory)
$scope.common = Common
$scope.master = {} # Initialise our main object hash
investor_id = $routeParams.investor_id # Get the ID of the investor we are editing from the URL
# Get the investor information & assign it to the scope master object
console.log 'Get JSON'
$scope.investor = new Investor.show({investor_id: investor_id}, (resource) ->
# copy the response from server response (JSON format) to the scopes master
$scope.master = angular.copy(resource)
# Convert price_cents to dollars
$scope.investor.price_cents /= 100
)
# Update the investor passing the JSON back to the server.
$scope.update = (investor) ->
# Convert price_cents to cents
investor.price_cents *= 100
$scope.master = angular.copy(investor)
investor.$update({investor_id: investor_id}, (t) ->
$location.path('/investors/' + t.id)
)
これを行うより良い方法はありますか?