PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/WS.EKA.Portal/Scripts/models/order/invoice.js

http://mobileshop.codeplex.com
JavaScript | 127 lines | 117 code | 10 blank | 0 comment | 16 complexity | 31af229366f22c1aefca327d146215a8 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. window.Invoice = Backbone.Model.extend({
  2. InvoiceTypeId: 1,
  3. InvoiceType: '',
  4. InvoiceHeader: '',
  5. InvoiceContent: '',
  6. HasInvoice: true
  7. });
  8. window.InvoiceType = Backbone.Model.extend({});
  9. window.InvoiceTypeList = Backbone.Collection.extend({
  10. model: InvoiceType,
  11. urlRoot: 'api/order/invoicetype/list',
  12. url: function () {
  13. return this.urlRoot;
  14. }
  15. });
  16. window.InvoiceView = Backbone.View.extend({
  17. el: '#jqt',
  18. initialize: function () {
  19. this.template = $('#InvoiceTemplate').html();
  20. this.invoiceType = new InvoiceTypeList();
  21. },
  22. events: {
  23. 'click .myinvoice .toggle': 'toggleInvoiceTemplate',
  24. 'click .myinvoice #InvoiceBtn': 'saveInvoice'
  25. },
  26. render: function () {
  27. var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(),
  28. innerFooter: $('#InnerFooterTemplate').html()
  29. };
  30. var data = { hasBack: true, title: "??", btnListR: [{ name: 'home'}] };
  31. this.$el.append(Mustache.render(this.template, data, partial));
  32. this.fetchInvoiceType();
  33. return this;
  34. },
  35. fetchInvoiceType: function () {
  36. var that = this;
  37. $.getJSON('api/order/invoicetype/list', function (res) {
  38. var template = $('#InvoiceTypeTemplate').html();
  39. if (res.length > 0) {
  40. var data = [];
  41. for (i in res) {
  42. data.push({id:i,type:res[i]});
  43. }
  44. $('.invoiceTypeList').html(Mustache.render(template, { invoicetypes: data }, []));
  45. }
  46. that.setDefaultValue();
  47. pageView.resizeScroll();
  48. });
  49. },
  50. setDefaultValue: function () {
  51. var invoice = localStorage.getItem('invoiceitem');
  52. if (invoice) {
  53. var invoiceItem = JSON.parse(invoice);
  54. if (!invoiceItem.HasInvoice) {
  55. $('.myinvoice input[type="checkbox"]')[0].checked = false;
  56. $('.invoiceTemplate').hide();
  57. } else {
  58. $('.myinvoice input[type="checkbox"]')[0].checked = true;
  59. $('.invoiceTemplate').show();
  60. $('.invoiceTemplate input[type="radio"][value="' + invoiceItem.InvoiceTypeId + '"]')[0].checked = true;
  61. $('#invoice_headertype').val(invoiceItem.InvoiceHeader);
  62. $('#invoice_content').val(invoiceItem.InvoiceContent);
  63. }
  64. }
  65. },
  66. toggleInvoiceTemplate: function (e) {
  67. var target = $(e.currentTarget),
  68. checkbox = target.find('input[type="checkbox"]');
  69. if (!checkbox[0].checked) {
  70. $('.invoiceTemplate').hide();
  71. } else {
  72. $('.invoiceTemplate').show();
  73. }
  74. pageView.resizeScroll();
  75. },
  76. saveInvoice: function (e) {
  77. var checkbox = $('.myinvoice input[type="checkbox"]'),
  78. invoicetype = $('.myinvoice input[type="radio"]:checked'),
  79. invoicehead = $('#invoice_headertype').val().trim(),
  80. invoicecontent = $('#invoice_content').val().trim();
  81. if (checkbox[0].checked) {
  82. if (this.validation()) {
  83. var invoice = new Invoice({
  84. InvoiceTypeId: invoicetype.val(),
  85. InvoiceType: invoicetype.next('label').text(),
  86. InvoiceHeader: invoicehead,
  87. InvoiceContent: invoicecontent,
  88. HasInvoice: true
  89. });
  90. localStorage.setItem('invoiceitem', JSON.stringify(invoice));
  91. pageView.goBack();
  92. }
  93. } else {
  94. var invoice = new Invoice({
  95. HasInvoice: false
  96. });
  97. localStorage.setItem('invoiceitem', JSON.stringify(invoice));
  98. pageView.goBack();
  99. }
  100. },
  101. validation: function () {
  102. var flag = true,
  103. invoicehead = $('#invoice_headertype').val().trim(),
  104. invoicecontent = $('#invoice_content').val().trim();
  105. if (invoicehead == "") {
  106. $('#head_error').text('??????'); flag = false;
  107. } else {
  108. $('#head_error').text(''); flag = true;
  109. }
  110. if (invoicehead == "") {
  111. $('#content_error').text('??????'); flag = false;
  112. } else {
  113. $('#content_error').text(''); flag = true;
  114. }
  115. return flag;
  116. }
  117. });