PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/public/javascripts/app.js

https://bitbucket.org/jszynal/gnw
JavaScript | 183 lines | 107 code | 41 blank | 35 comment | 5 complexity | d85f4425ef0403cfba9dd6ad85abc5e9 MD5 | raw file
  1. /*
  2. Visit http://documentcloud.github.com/backbone for usage details
  3. concerning Backbone.js, an awesome client-side MVC javascript framework
  4. used here.
  5. */
  6. /************************/
  7. /* Global App Object */
  8. /************************/
  9. var GNWApp = GNWApp || {};
  10. GNWApp.models = GNWApp.models || {};
  11. GNWApp.collections = GNWApp.collections || {};
  12. GNWApp.views = GNWApp.views || {};
  13. GNWApp.util = GNWApp.util || {};
  14. // We use CSRF protection from Express. A unique CSRF token
  15. // can be found in META tag on every page. On every Backbone.js
  16. // client-side model sync (create/update/delete), we include
  17. // this CSRF token with sent data
  18. Backbone.Model.prototype.toJSON = function(){
  19. return _(_.clone(this.attributes)).extend({
  20. '_csrf' : $('meta[name="csrf-token"]').attr('content')
  21. });
  22. };
  23. /************************/
  24. /* Models/Collections */
  25. /************************/
  26. GNWApp.models.User = Backbone.Model.extend({
  27. // urlRoot : this is like the base REST url for Backbone.js sync()
  28. // calling .save() on new model will POST here, existing
  29. // model will PUT here.
  30. urlRoot: '/api/user',
  31. // idAttribute : changed from 'id' Backbone default to '_id' becuase that's what
  32. // MongoDB documents use as their ID making Backbone.Model -> MongoDB Doc
  33. // cleaner.
  34. idAttribute: '_id'
  35. });
  36. GNWApp.models.Policy = Backbone.Model.extend({
  37. urlRoot: '/api/policy',
  38. idAttribute: '_id'
  39. });
  40. GNWApp.collections.Policies = Backbone.Collection.extend({
  41. model: GNWApp.models.Policy
  42. });
  43. /************************/
  44. /* Views */
  45. /************************/
  46. GNWApp.views.HomeView = Backbone.View.extend({
  47. el: 'body',
  48. events: {
  49. 'click a#save-btn' : 'createPolicyHandler',
  50. 'click a#cancel-btn' : 'cancelPolicy'
  51. },
  52. initialize: function(){
  53. this._policyContainer = self.$('#policy-container');
  54. this.renderPolicies();
  55. },
  56. createPolicyHandler: function(e){
  57. var self = this;
  58. var p = new GNWApp.models.Policy({customer: self.$('#customer').val(), type: self.$('#policy-type').val(), description: self.$('#policy-description').val()});
  59. p.save({},{
  60. success: function(model,res){
  61. console.dir(res);
  62. self.resetPolicyForm();
  63. self.$('#policy-form-modal').modal('hide');
  64. },
  65. error: function(){
  66. console.log('there was an error');
  67. }
  68. });
  69. e.preventDefault();
  70. },
  71. cancelPolicy: function(e){
  72. this.resetPolicyForm();
  73. this.$('#policy-form-modal').modal('hide');
  74. e.preventDefault();
  75. },
  76. resetPolicyForm: function(){
  77. this.$('#customer').val('');
  78. this.$('#policy-description').val('');
  79. },
  80. renderPolicies: function(){
  81. var self = this;
  82. this.collection.each(function(policy){
  83. var view = new GNWApp.views.Policy({model: policy});
  84. view.render().appendTo(self._policyContainer);
  85. });
  86. }
  87. });
  88. GNWApp.views.Policy = Backbone.View.extend({
  89. tagName: 'div',
  90. className: 'policy-item',
  91. initialize: function(){
  92. this._viewTmpl = $('#mainPolicyTmpl');
  93. },
  94. render: function(){
  95. var self = this;
  96. $(self.el).html(self._viewTmpl.tmpl(self.model.attributes));
  97. return $(self.el);
  98. }
  99. });
  100. GNWApp.views.Account = Backbone.View.extend({
  101. el: 'div#account-screen',
  102. events: {
  103. },
  104. initialize: function(){
  105. },
  106. });
  107. /************************/
  108. /* Util */
  109. /************************/
  110. GNWApp.config = {
  111. initSockets: function(){
  112. var socket = io.connect('http://localhost');
  113. socket.on('news', function (data) {
  114. console.log(data);
  115. socket.emit('my other event', { my: 'data' });
  116. });
  117. }
  118. };
  119. /************************/
  120. /* App Entry Point */
  121. /************************/
  122. GNWApp.run = function(){
  123. var MainView, AccountView, screedId;
  124. screenId = $('body').attr('id'); // ID attrib of body tag used to identify which screen
  125. switch(screenId){
  126. case 'home':
  127. window.HomeView = new GNWApp.views.HomeView({collection: new GNWApp.collections.Policies(GNWPOLICIES.data)});
  128. break;
  129. case 'account':
  130. AccountView = new GNWApp.views.Account();
  131. //console.log('account page');
  132. break;
  133. default:
  134. //console.log('other page');
  135. }
  136. };
  137. /************************/
  138. /* Fire on DOM ready */
  139. /************************/
  140. $(function(){
  141. GNWApp.run();
  142. });