PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/backbone.md

https://github.com/voxiness/guidelines
Markdown | 150 lines | 110 code | 40 blank | 0 comment | 0 complexity | efd939e9881a7ba7b9bc2652a4c2f30a MD5 | raw file
  1. The Backbone Style Guide
  2. ====================
  3. ## Table of contents
  4. * [Naming](#naming)
  5. * [Namespacing](#namespacing)
  6. * [File Organization](#file-organization)
  7. ## Naming
  8. Use `camelCase` for variables and functions
  9. ```javascript
  10. // Bad
  11. var my_var = [2, 1]
  12. // Good
  13. var myVar = [2, 1]
  14. ```
  15. ```javascript
  16. // Bad
  17. function my_function(args){
  18. ...
  19. }
  20. // Good
  21. function myFunction(args){
  22. ...
  23. }
  24. ```
  25. Use `CapitalizedWords` for classes and namespaces
  26. ```javascript
  27. // Bad
  28. app.views.search_property = Backbone.View.extend({})
  29. // Good
  30. App.Views.SearchProperty = Backbone.View.extend({})
  31. ```
  32. Do not use {model|view|collection} when naming `models`, `views` or `collections`
  33. ```javascript
  34. // Bad
  35. App.Views.SearchPropertyView = Backbone.View.extend({})
  36. // Good
  37. App.Views.SearchProperty = Backbone.View.extend({})
  38. ```
  39. Use singular for `model` classes and plural for `collection` classes
  40. ```javascript
  41. App.Models.Property = Backbone.Model.extend({})
  42. App.Collections.Properties = Backbone.Collection.extend({})
  43. ```
  44. Use singular or plural for `views` depending if you passing a model or collection
  45. ```javascript
  46. App.Views.SearchProperty = Backbone.View.extend({})
  47. new App.Views.SearchProperty({
  48. model: new App.Models.Property(...)
  49. });
  50. App.Views.SearchProperties = Backbone.View.extend({})
  51. new App.Views.SearchProperties({
  52. collection: [new App.Models.Property(...)]
  53. })
  54. ```
  55. ## Namespacing
  56. Always use a root element to not pollute the window namespace
  57. ```javascript
  58. // Bad
  59. SearchProperty = ...
  60. // Still not perfect though
  61. App.SearchProperty = ...
  62. ```
  63. Always use namespaces under the root element to reference backbone components as `Models`, `Views` or `Collections`.
  64. ```javascript
  65. // Bad
  66. App.SearchProperty = Backbone.View.extend({})
  67. // Good
  68. App.Views.SearchProperty = Backbone.View.extend({})
  69. ```
  70. Have exactly 1 file that contains all namespace initialization and bind the root namespace to `window`
  71. ```javascript
  72. window.App = {}
  73. App.Models = {}
  74. App.Collections = {}
  75. App.Views = {}
  76. ```
  77. ## File Organization
  78. Assuming `root` is where you have your javascript assets, the file organization should follow these rules:
  79. ```
  80. <root>
  81. ├── app.js # initialize namespaces here
  82. ├── lib
  83. ├── lib1.min.js
  84. ├── lib2.min.js
  85. └── lib3.min.js
  86. ├── models
  87. ├── model1.js
  88. └── model2.js
  89. ├── collections
  90. ├── collections1.js
  91. └── collections2.js
  92. ├── views
  93. ├── context1
  94. ├── view1.js
  95. └── view2.js
  96. ├── context2
  97. ├── view1.js
  98. └── view2.js
  99. ```
  100. If using Rails:
  101. * the 3rd party code (jQuery, Backbone, Underscore, etc) should be in `/vendor/assets/javascripts`
  102. * the application code should be in `/app/assets/javascripts`
  103. Requiring the files should follow this order
  104. ```
  105. lib/*
  106. app.js
  107. models/*
  108. collections/*
  109. views/*
  110. ```