Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. this.extendedPrice = ko.computed(function () {
  2. return this.product()
  3. ? this.product().salesPrice() * 0.8 + parseFloat(this.product().salesPrice())
  4. : 0;
  5. },
  6.  
  7. this.extendedPrice = ko.computed({
  8. read: function () {
  9. return this.product()
  10. ? this.product().salesPrice() * 0.8 + parseFloat(this.product().salesPrice())
  11. : 0;
  12. },
  13. owner: this,
  14. });
  15.  
  16. <input data-bind="visible:product, value:extendedPrice"/>
  17.  
  18. <input data-bind="visible:product, value:extendedPrice()"/>
  19.  
  20. this.extendedPrice = <KnockoutComputed<number>>ko.computed({
  21. read: function () {
  22. return this.product()
  23. ? this.product().salesPrice() * 0.8 + parseFloat(this.product().salesPrice())
  24. : 0;
  25. },
  26.  
  27. write: function (value: string) {
  28. var num = parseFloat(value.replace(/[^.d]/g,""));
  29. num = isNaN(num) ? 0 : num;
  30. var unitPrice = num / this.quantity();
  31. this.product().salesPrice(unitPrice);
  32. },
  33.  
  34. owner: this,
  35. });
  36.  
  37. ///<reference path="data.ts" />
  38. ///<reference path="../typedefinitions/jquery.d.ts" />
  39. ///<reference path="../typedefinitions/knockout.d.ts" />
  40.  
  41. module guitarsalesportalmodule {
  42. export class Mod {
  43. constructor(public Name: string, public Id: number) { }
  44. }
  45.  
  46. export class Cat {
  47. constructor(public Name: string, public Id: number) { }
  48. }
  49.  
  50. export class Product {
  51. salesPrice: KnockoutObservable<number>;
  52.  
  53. constructor(public modelId: number, sp: number, public listPrice: number, public rating: number, public photo: string, public description: string, public model: Mod, public category: Cat) {
  54. this.salesPrice = ko.observable(sp);
  55. }
  56. }
  57.  
  58. export class LineItem {
  59. product: KnockoutObservable<Product>;
  60. quantity: KnockoutObservable<number>;
  61. extendedPrice: KnockoutComputed<number>;
  62.  
  63. constructor(product: Product, quantity: number) {
  64. this.product = ko.observable(product);
  65. this.quantity = ko.observable(quantity);
  66. this.extendedPrice = <KnockoutComputed<number>>ko.computed({
  67. read: ()=>{
  68. return this.product()
  69. ? this.product().salesPrice() * 0.8 + parseFloat(this.product().salesPrice().toString())/*Just leaving it as this.product().salesPrice() results in it being considered string and getting concatenated. Doing parseFloat(this.product().salesPrice().toString()) instead of parseFloat(this.product().salesPrice()) strangely doesn't work either saying it's not a string but a number(doesn't happen when I use function instead of ()=>).*/
  70. : 0;
  71. },
  72.  
  73. write: (value: string) => {
  74. var num = parseFloat(value.replace(/[^.d]/g,""));
  75. num = isNaN(num) ? 0 : num;
  76. var unitPrice = num / this.quantity();
  77. this.product().salesPrice(unitPrice);
  78. },
  79.  
  80. owner: this,
  81. });
  82. }
  83. }
  84.  
  85. export class Vm {
  86. products: KnockoutObservableArray<Product>;
  87. lines: KnockoutObservableArray<LineItem>;
  88. grandTotal: KnockoutComputed<number>;
  89.  
  90. constructor() {
  91. this.loadProducts();
  92. this.lines = ko.observableArray([new LineItem(undefined, undefined)]);
  93. this.grandTotal = ko.computed(
  94. () => {
  95. var total = 0;
  96. this.lines().forEach((line) => {
  97. if (line.extendedPrice() && line.quantity()) {
  98. total += line.extendedPrice() * line.quantity();
  99. }
  100. });
  101. return total;
  102. },
  103. this);
  104. }
  105.  
  106. loadProducts() {
  107. this.products = ko.observableArray([]);
  108. $.each(data.Products, (i) => {
  109. this.products.push(new Product(
  110. data.Products[i].ModelId,
  111. data.Products[i].SalePrice,
  112. data.Products[i].ListPrice,
  113. data.Products[i].Rating,
  114. data.Products[i].Photo,
  115. data.Products[i].Description,
  116. data.Products[i].Model,
  117. data.Products[i].Category));
  118. });
  119. }
  120.  
  121. addLineItem = () => {
  122. this.lines.push(new LineItem(undefined, undefined));
  123. }
  124.  
  125. removeLineItem = (line) => {
  126. this.lines.remove(line);
  127. }
  128.  
  129. formatCurrency(curr: string) {
  130. return "$" + parseFloat(curr).toFixed(2);
  131. }
  132. }
  133.  
  134. export var viewModel = new Vm();
  135. }
  136.  
  137. //apply bindings
  138. window.onload = () => {
  139. ko.applyBindings(guitarsalesportalmodule.viewModel);
  140. };
  141.  
  142. <!DOCTYPE html>
  143. <html xmlns="http://www.w3.org/1999/xhtml">
  144. <head>
  145. <title></title>
  146. <link href="../css/fonts.css" rel="stylesheet" />
  147. <link href="../css/styles.css" rel="stylesheet" />
  148. <script src="../scripts/jquery-2.1.0.js"></script>
  149. <script src="../scripts/knockout-3.1.0.js"></script>
  150. <script src="../scripts/data.js"></script>
  151. <script src="../scripts/guitarsalesportal.js"></script>
  152. </head>
  153. <body>
  154. <div class="showroom">
  155. <table>
  156. <tr>
  157. <th>Product</th>
  158. <th>Price</th>
  159. <th>Quantity</th>
  160. <th>Extended Price</th>
  161. <th>Remove</th>
  162. </tr>
  163. <tbody data-bind="foreach: lines">
  164. <tr>
  165. <td>
  166. <select data-bind="options:$parent.products, value:product, optionsText:'description', optionsCaption:'Choose...'"></select>
  167. </td>
  168. <td data-bind="if:product">
  169. <input data-bind="value:product().salesPrice"/>
  170. </td>
  171. <td>
  172. <input data-bind="visible:product, value:quantity"/>
  173. </td>
  174. <td>
  175. <input data-bind="visible:product, value:extendedPrice()"/>
  176. <!--<input data-bind="visible:product, value:extendedPrice"/>-->
  177. </td>
  178. <td>
  179. <a href="#" data-bind="click: $parent.removeLineItem">Remove</a>
  180. </td>
  181. </tr>
  182. </tbody>
  183. </table>
  184. <a href="#" data-bind="click: addLineItem">Add</a>
  185. <div>Grand Total: <span data-bind="text:formatCurrency(grandTotal()), valueUpdate:'afterkeydown'"></span></div>
  186. </div>
  187. </body>
  188. </html>
  189.  
  190. this.extendedPrice = ko.computed( () => { // Notice () =>
  191. return this.product()
  192. ? this.product().salesPrice() * 0.8 + parseFloat(this.product().salesPrice())
  193. : 0;
  194. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement