Skip to main content

The ChangeQuantity Method 3

Now all we need to do is update the value of the TotalCharge field of the order form by sending a line of JavaScript code back to the browser. To do this we use the &js<> directive within our method. Compiling a class converts the contents of a &js<> to the appropriate sequence of ObjectScript Write commands to return JavaScript to a browser.

We use the $FN function to format the total value with two decimal places. Next, we use the QuoteJS method to quote the formatted value as a JavaScript string. (Without this, the value is treated directly as a JavaScript string and loses the formatting applied by $FN.)

Note that we must save the modified TicketOrder object to the database. Note also that QuoteJS is a class method contained in the %CSP.PageOpens in a new tab class. In order to invoke the method within Cinema.UtilsOpens in a new tab, we must use the ##class(PackageName.ClassName).MethodName() syntax.

—Utils.ChangeQuantity—
Utils.ChangeQuantity
ClassMethod ChangeQuantity(
    ItemNum As %Integer, 
    TicketType As %Integer, 
    NewQuantity As %Integer)
{
  If $data(%session.Data("Order")) 
  {
      // Open the current order object
      Set ord = ##class(Cinema.TicketOrder).%OpenId(%session.Data("Order"))

      // Update quantity
      Set itm=ord.Items.GetAt(ItemNum)

      // Determine if we are changing
      // adult or child tickets
      If ( TicketType = 1) 
      {
          // Adjust the total price
          Set ord.Total = ord.Total +
            ((NewQuantity - itm.AdultTickets) * itm.Show.Theater.AdultPrice)
          // Update the number of tickets
          Set itm.AdultTickets = NewQuantity
      }
      Else 
      {
          Set ord.Total = ord.Total +
            ((NewQuantity - itm.ChildTickets) * itm.Show.Theater.ChildPrice)
          Set itm.ChildTickets = NewQuantity
      }
      &js<parent.Order.document.OrderTickets.TotalCharge.value=
         #(##class(%CSP.Page).QuoteJS($FN(ord.Total,"",2)))#;>

      // Save incomplete order.
      Do ord.%Save()
  }
}
FeedbackOpens in a new tab