使用coffeescript对模态进行计算 [英] Making a calculation for a modal using coffeescript

查看:112
本文介绍了使用coffeescript对模态进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让用户可以输入3个数字,当他们点击提交按钮时,他们将看到一个模态,其中包含模态中这3个数字的计算结果。我不确定如何做到这一点。我在我的控制器中进行了计算。

I am trying to make it so a user can input 3 numbers and when they click the submit button they will see a modal with the calculations from these 3 numbers within the modal. I am unsure how to accomplish this. I have the calculation in my controller.

class WelcomeController < ApplicationController

  def how_much
    @price = (params[:amount])
    @mortgage = (params[:high_rent])
    @rent = (params[:current_rent])

    if @price && @mortgage && @rent.present?
      @monthly_savings = @mortgage - @rent
      @savings_goal = @price*0.03
      @months_to_buy = (@savings_goal/@monthly_savings).to_i
      @total_savings = @monthly_savings * @months_to_buy
    else
      @months_to_buy = 24
      @total_savings = "great savings"
    end
    respond_to do |format|
        format.json { render json: {:months_to_buy => @months_to_buy, :total_savings => @total_savings}}
      end
    end

end

我的表格如下...

      <%= form_tag( '/welcome/how_much', post: true, remote: true) do %>
       <h5 class="label">Estimated new home cost?</h5>
        <%= text_field_tag 'price', nil, placeholder: 'ex. 100,000', class: "form-control form-control-lg" %>

       <h5 class="label">Estimated payment for a new home?</h5>
        <%= text_field_tag 'mortgage', nil, placeholder: 'ex. 1,200', class: "form-control form-control-lg" %>

       <h5 class="label">Current Monthly Rent?</h5>
        <%= text_field_tag 'rent', nil, placeholder: 'ex. 800', class: "form-control form-control-lg" %>

       <button type="button" class="btn btn-success btn-lg" data-toggle="modal" data-target="#savingsModal">
      See how we help
    </button>

<!-- Modal for sign-up -->
<div class="modal" id="savings_modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <h3 class="modal-title" id="savingsModalTitle">You could be ready to buy in <%= @months_to_buy %> months</h3>
      <h5 class="modal-title">and have <%= @total_savings %>* to put towards a down payment & ...</h5>
      <div class="modal-body">
        <h4>Sign-up Now to get started!</h4>
      </div>
    </div>
  </div>
</div>
<% end %>

最后,这里是我的CoffeeScript。它也有计算,但我不确定是否应该在CoffeeScript或控制器中进行计算。我不知道如果我是这样做的,或者我是完全关闭基地,请帮助!!!!

Finally, here is the CoffeeScript I have. It has the calculations as well but I am unsure if I should do the calculations in the CoffeeScript or in the controller. I have no idea if I am doing this right or I am COMPLETELY OFF BASE, PLEASE HELP!!!!

# Calculate Savings jQuery
price = document.getElementsByName('house_amount')[0].value
mortgage = document.getElementsByName('high_rent')[0].value
rent = document.getElementsByName('current_rent')[0].value
MonthlySavings: (mortgage, rent) ->
 if mortgage? && rent?
   parseFloat(mortgage) - parseFloat(rent)
SavingsGoal: (price) ->
 if price?
   parseFloat(price) * 0.03
MonthsToBuy: (Savings_goal,MonthlySavings) ->
 if SavingsGoal? && MonthlySavings?
   parseFloat(SavingsGoal)/parseFloat(MonthlySavings)
TotalSavings: (MonthlySavings,MonthsToBuy) ->
 if MonthlySavings? && MonthsToBuy?
   parseFloat(MonthlySavings) * parseFloat(MonthsToBuy)


推荐答案

class Calculation
  constructor (@price, @mortgage, @rent) -> 
  monthly_savings -> @mortgage - @rent
  savings_goal -> @price * 0.03
  months -> Math.ceil @savings_goal / @monthly_savings
  savings -> monthly_savings * months

然后创建计算实例:

And to create the calculaction instance:

price = document.getElementsByName('house_amount')[0].value
mortgage = document.getElementsByName('high_rent')[0].value
rent = document.getElementsByName('current_rent')[0].value
instance = new Calculation(price, mortgage, rent)

然后在某处显示它:

Then to display it somewhere:

document.getElementById('total_savings').value = instance.savings();

这篇关于使用coffeescript对模态进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆