ninjs 0.14.1 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. data/.bundle/config +2 -2
  2. data/.travis.yml +1 -0
  3. data/Gemfile +1 -0
  4. data/Gemfile.lock +13 -11
  5. data/README.md +108 -93
  6. data/Rakefile +17 -7
  7. data/VERSION +1 -1
  8. data/bin/ninjs +24 -22
  9. data/lib/ninjs/command.rb +32 -21
  10. data/lib/ninjs/configuration.rb +35 -53
  11. data/lib/ninjs/generator.rb +6 -6
  12. data/lib/ninjs/notification.rb +5 -5
  13. data/lib/ninjs/project.rb +31 -25
  14. data/ninjs.gemspec +18 -142
  15. data/repository/ninjs/core/application.js +13 -24
  16. data/repository/ninjs/core/dom.js +87 -68
  17. data/repository/ninjs/core/existence.js +44 -105
  18. data/repository/ninjs/core/extend.js +11 -32
  19. data/repository/ninjs/core/module.js +53 -107
  20. data/repository/ninjs/core/nin.js +5 -7
  21. data/repository/ninjs/tests/extension.test.js +0 -10
  22. data/repository/ninjs/tests/index.html +19 -20
  23. data/repository/ninjs/tests/module.test.js +52 -56
  24. data/repository/ninjs/utilities/array.js +8 -8
  25. data/repository/ninjs/utilities/cookie.js +59 -0
  26. data/repository/ninjs/utilities/css.js +7 -7
  27. data/repository/ninjs/utilities/number.js +2 -2
  28. data/repository/ninjs/utilities/string.js +26 -26
  29. data/spec/cli_spec.rb +90 -88
  30. data/spec/command_spec.rb +104 -137
  31. data/spec/configuration_spec.rb +23 -37
  32. data/spec/fixtures/_global.module.js +9 -0
  33. data/spec/fixtures/compressed.myapp.js +23 -44
  34. data/spec/fixtures/foo.elements.js +1 -1
  35. data/spec/fixtures/foo.js +5 -5
  36. data/spec/fixtures/foo.model.js +1 -1
  37. data/spec/fixtures/foo.module.js +3 -3
  38. data/spec/fixtures/hello.elements.js +1 -1
  39. data/spec/fixtures/hello.js +5 -5
  40. data/spec/fixtures/hello.model.js +1 -1
  41. data/spec/fixtures/hello.module.js +3 -3
  42. data/spec/fixtures/myapp.initial.js +198 -329
  43. data/spec/fixtures/myapp.js +285 -357
  44. data/spec/fixtures/mymodule.alias.module.js +2 -2
  45. data/spec/fixtures/mymodule.dependencies.module.js +2 -2
  46. data/spec/fixtures/mymodule.elements.js +1 -1
  47. data/spec/fixtures/mymodule.model.js +1 -1
  48. data/spec/fixtures/mymodule.module.js +2 -2
  49. data/spec/fixtures/mymodule.module_alias.module.js +10 -0
  50. data/spec/fixtures/new.ninjs.conf +2 -0
  51. data/spec/fixtures/nin.js +198 -329
  52. data/spec/fixtures/ninjs.conf +2 -0
  53. data/spec/fixtures/updated.ninjs.conf +8 -0
  54. data/spec/generator_spec.rb +37 -26
  55. data/spec/ninjs_spec.rb +6 -6
  56. data/spec/project_spec.rb +229 -221
  57. data/spec/spec_helper.rb +3 -1
  58. data/spec/testspec_spec.rb +7 -0
  59. metadata +28 -145
  60. data/repository/json2.js +0 -482
  61. data/repository/modernizr/1.5.js +0 -28
  62. data/repository/modernizr/latest.js +0 -1
  63. data/repository/ninjs/docs/Data/ClassHierarchy.nd +0 -0
  64. data/repository/ninjs/docs/Data/ConfigFileInfo.nd +0 -0
  65. data/repository/ninjs/docs/Data/FileInfo.nd +0 -16
  66. data/repository/ninjs/docs/Data/ImageFileInfo.nd +0 -0
  67. data/repository/ninjs/docs/Data/ImageReferenceTable.nd +0 -0
  68. data/repository/ninjs/docs/Data/IndexInfo.nd +0 -0
  69. data/repository/ninjs/docs/Data/PreviousMenuState.nd +0 -0
  70. data/repository/ninjs/docs/Data/PreviousSettings.nd +0 -0
  71. data/repository/ninjs/docs/Data/SymbolTable.nd +0 -0
  72. data/repository/ninjs/docs/Languages.txt +0 -113
  73. data/repository/ninjs/docs/Menu.txt +0 -63
  74. data/repository/ninjs/docs/Topics.txt +0 -81
  75. data/repository/ninjs/docs/files/core/application-js.html +0 -44
  76. data/repository/ninjs/docs/files/core/existence-js.html +0 -60
  77. data/repository/ninjs/docs/files/core/extend-js.html +0 -55
  78. data/repository/ninjs/docs/files/core/module-js.html +0 -80
  79. data/repository/ninjs/docs/files/core/nin-js.html +0 -38
  80. data/repository/ninjs/docs/index.html +0 -1
  81. data/repository/ninjs/docs/index/Classes.html +0 -33
  82. data/repository/ninjs/docs/index/Files.html +0 -45
  83. data/repository/ninjs/docs/index/Functions.html +0 -65
  84. data/repository/ninjs/docs/index/General.html +0 -85
  85. data/repository/ninjs/docs/index/Variables.html +0 -45
  86. data/repository/ninjs/docs/javascript/main.js +0 -841
  87. data/repository/ninjs/docs/javascript/prettify.js +0 -1526
  88. data/repository/ninjs/docs/javascript/searchdata.js +0 -152
  89. data/repository/ninjs/docs/search/ClassesN.html +0 -20
  90. data/repository/ninjs/docs/search/FilesA.html +0 -20
  91. data/repository/ninjs/docs/search/FilesE.html +0 -20
  92. data/repository/ninjs/docs/search/FilesM.html +0 -20
  93. data/repository/ninjs/docs/search/FilesN.html +0 -20
  94. data/repository/ninjs/docs/search/FunctionsA.html +0 -20
  95. data/repository/ninjs/docs/search/FunctionsC.html +0 -20
  96. data/repository/ninjs/docs/search/FunctionsE.html +0 -20
  97. data/repository/ninjs/docs/search/FunctionsI.html +0 -20
  98. data/repository/ninjs/docs/search/FunctionsM.html +0 -20
  99. data/repository/ninjs/docs/search/FunctionsR.html +0 -20
  100. data/repository/ninjs/docs/search/FunctionsS.html +0 -20
  101. data/repository/ninjs/docs/search/FunctionsSymbols.html +0 -20
  102. data/repository/ninjs/docs/search/FunctionsU.html +0 -20
  103. data/repository/ninjs/docs/search/GeneralA.html +0 -20
  104. data/repository/ninjs/docs/search/GeneralC.html +0 -20
  105. data/repository/ninjs/docs/search/GeneralD.html +0 -20
  106. data/repository/ninjs/docs/search/GeneralE.html +0 -20
  107. data/repository/ninjs/docs/search/GeneralF.html +0 -20
  108. data/repository/ninjs/docs/search/GeneralI.html +0 -20
  109. data/repository/ninjs/docs/search/GeneralM.html +0 -20
  110. data/repository/ninjs/docs/search/GeneralN.html +0 -20
  111. data/repository/ninjs/docs/search/GeneralR.html +0 -20
  112. data/repository/ninjs/docs/search/GeneralS.html +0 -20
  113. data/repository/ninjs/docs/search/GeneralSymbols.html +0 -20
  114. data/repository/ninjs/docs/search/GeneralT.html +0 -20
  115. data/repository/ninjs/docs/search/GeneralU.html +0 -20
  116. data/repository/ninjs/docs/search/GeneralV.html +0 -20
  117. data/repository/ninjs/docs/search/NoResults.html +0 -15
  118. data/repository/ninjs/docs/search/VariablesD.html +0 -20
  119. data/repository/ninjs/docs/search/VariablesN.html +0 -20
  120. data/repository/ninjs/docs/search/VariablesR.html +0 -20
  121. data/repository/ninjs/docs/search/VariablesT.html +0 -20
  122. data/repository/ninjs/docs/styles/main.css +0 -828
  123. data/repository/nullconsole.js +0 -7
  124. data/repository/qunit/assets/css/qunit.css +0 -196
  125. data/repository/qunit/qunit.js +0 -1374
  126. data/repository/selectivizr/1.0.js +0 -5
  127. data/repository/selectivizr/latest.js +0 -1
  128. data/repository/syntaxhighlighter/all.js +0 -11
  129. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCore.css +0 -226
  130. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreDefault.css +0 -328
  131. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreDjango.css +0 -331
  132. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreEclipse.css +0 -339
  133. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreEmacs.css +0 -324
  134. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  135. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreMDUltra.css +0 -324
  136. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreMidnight.css +0 -324
  137. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreRDark.css +0 -324
  138. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeDefault.css +0 -117
  139. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeDjango.css +0 -120
  140. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeEclipse.css +0 -128
  141. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeEmacs.css +0 -113
  142. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  143. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeMDUltra.css +0 -113
  144. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeMidnight.css +0 -113
  145. data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeRDark.css +0 -113
  146. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCore.scss +0 -216
  147. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreDefault.scss +0 -2
  148. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreDjango.scss +0 -2
  149. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreEclipse.scss +0 -2
  150. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreEmacs.scss +0 -2
  151. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreFadeToGrey.scss +0 -2
  152. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreMDUltra.scss +0 -2
  153. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreMidnight.scss +0 -2
  154. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreRDark.scss +0 -2
  155. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeDefault.scss +0 -7
  156. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeDjango.scss +0 -36
  157. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeEclipse.scss +0 -48
  158. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeEmacs.scss +0 -32
  159. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeFadeToGrey.scss +0 -36
  160. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeMDUltra.scss +0 -32
  161. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeMidnight.scss +0 -32
  162. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeRDark.scss +0 -32
  163. data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_theme_template.scss +0 -120
  164. data/repository/syntaxhighlighter/shAutoloader.js +0 -17
  165. data/repository/syntaxhighlighter/shBrushAS3.js +0 -59
  166. data/repository/syntaxhighlighter/shBrushAppleScript.js +0 -75
  167. data/repository/syntaxhighlighter/shBrushBash.js +0 -59
  168. data/repository/syntaxhighlighter/shBrushCSharp.js +0 -65
  169. data/repository/syntaxhighlighter/shBrushColdFusion.js +0 -100
  170. data/repository/syntaxhighlighter/shBrushCpp.js +0 -97
  171. data/repository/syntaxhighlighter/shBrushCss.js +0 -91
  172. data/repository/syntaxhighlighter/shBrushDelphi.js +0 -55
  173. data/repository/syntaxhighlighter/shBrushDiff.js +0 -41
  174. data/repository/syntaxhighlighter/shBrushErlang.js +0 -52
  175. data/repository/syntaxhighlighter/shBrushGroovy.js +0 -67
  176. data/repository/syntaxhighlighter/shBrushJScript.js +0 -52
  177. data/repository/syntaxhighlighter/shBrushJava.js +0 -57
  178. data/repository/syntaxhighlighter/shBrushJavaFX.js +0 -58
  179. data/repository/syntaxhighlighter/shBrushPerl.js +0 -72
  180. data/repository/syntaxhighlighter/shBrushPhp.js +0 -88
  181. data/repository/syntaxhighlighter/shBrushPlain.js +0 -33
  182. data/repository/syntaxhighlighter/shBrushPowerShell.js +0 -74
  183. data/repository/syntaxhighlighter/shBrushPython.js +0 -64
  184. data/repository/syntaxhighlighter/shBrushRuby.js +0 -55
  185. data/repository/syntaxhighlighter/shBrushSass.js +0 -94
  186. data/repository/syntaxhighlighter/shBrushScala.js +0 -51
  187. data/repository/syntaxhighlighter/shBrushSql.js +0 -66
  188. data/repository/syntaxhighlighter/shBrushVb.js +0 -56
  189. data/repository/syntaxhighlighter/shBrushXml.js +0 -69
  190. data/repository/syntaxhighlighter/shCore.js +0 -17
  191. data/repository/syntaxhighlighter/shLegacy.js +0 -17
data/.bundle/config CHANGED
@@ -1,2 +1,2 @@
1
- ---
2
- BUNDLE_DISABLE_SHARED_GEMS: "1"
1
+ --- {}
2
+
data/.travis.yml ADDED
@@ -0,0 +1 @@
1
+ rvm: 1.9.2
data/Gemfile CHANGED
@@ -10,4 +10,5 @@ group :development do
10
10
  gem "jeweler", "~> 1.5.2"
11
11
  gem "rcov", ">= 0"
12
12
  gem "rspec", ">= 0"
13
+ gem "rdoc", ">= 0"
13
14
  end
data/Gemfile.lock CHANGED
@@ -1,24 +1,25 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- diff-lcs (1.1.2)
5
- fssm (0.2.2)
4
+ diff-lcs (1.1.3)
5
+ fssm (0.2.7)
6
6
  git (1.2.5)
7
7
  jeweler (1.5.2)
8
8
  bundler (~> 1.0.0)
9
9
  git (>= 1.2.5)
10
10
  rake
11
11
  jsmin (1.0.1)
12
- rake (0.8.7)
13
- rcov (0.9.9)
14
- rspec (2.4.0)
15
- rspec-core (~> 2.4.0)
16
- rspec-expectations (~> 2.4.0)
17
- rspec-mocks (~> 2.4.0)
18
- rspec-core (2.4.0)
19
- rspec-expectations (2.4.0)
12
+ rake (0.9.2)
13
+ rcov (0.9.10)
14
+ rdoc (3.9.4)
15
+ rspec (2.6.0)
16
+ rspec-core (~> 2.6.0)
17
+ rspec-expectations (~> 2.6.0)
18
+ rspec-mocks (~> 2.6.0)
19
+ rspec-core (2.6.4)
20
+ rspec-expectations (2.6.0)
20
21
  diff-lcs (~> 1.1.2)
21
- rspec-mocks (2.4.0)
22
+ rspec-mocks (2.6.0)
22
23
  shoulda (2.11.3)
23
24
  sprockets (1.0.2)
24
25
 
@@ -31,6 +32,7 @@ DEPENDENCIES
31
32
  jeweler (~> 1.5.2)
32
33
  jsmin
33
34
  rcov
35
+ rdoc
34
36
  rspec
35
37
  shoulda
36
38
  sprockets (= 1.0.2)
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  Readme
2
2
  ======
3
3
 
4
+ Current build status: [![Build Status](https://secure.travis-ci.org/daytonn/ninjs.png)](http://travis-ci.org/daytonn/ninjs)
5
+
4
6
  About
5
7
  -----
6
8
 
@@ -15,7 +17,7 @@ ninjs is written in Ruby (http://ruby-lang.org) and packaged as a Ruby gem (http
15
17
  Installation
16
18
  ------------
17
19
 
18
- You will need Ruby (1.8.x or 1.9.x) and RubyGems installed on your system to install ninjs using RubyGems. This is the easiest way of installing and recommended for most users.
20
+ You will need Ruby >= 1.9.2 and RubyGems installed on your system to install ninjs using RubyGems. This is the easiest way of installing and recommended for most users.
19
21
 
20
22
  ```sh
21
23
  gem install ninjs
@@ -24,7 +26,7 @@ gem install ninjs
24
26
  For development, clone the project and add bin/ninjs to your path (or my favorite trick which is to put it in my development folder and make symbolic link in /usr/local/bin or wherever you put your scripts):
25
27
 
26
28
  ```sh
27
- git clone git://github.com/textnotspeech/ninjs.git
29
+ git clone git://github.com/daytonn/ninjs.git
28
30
  ln -s ninjs/bin/ninjs /usr/local/bin/ninjs
29
31
  ```
30
32
 
@@ -33,10 +35,10 @@ Using the Command Line Application
33
35
 
34
36
  The ninjs command line application is a simple way to quickly develop and manage your application. With it you can create an application, generate scaffolding, compile, and upgrade your application.
35
37
 
36
- Create a ninjs application
38
+ Creating a ninjs application
37
39
  ==========================
38
40
 
39
- The first ninjs command you'll need is the "create" command. As it's name implies, this command creates a brand new ninjs application template, creating the necessary files and directories. The create command accepts two arguments. The first argument is the name of your application object which is required. The second argument, which is optional, is a sub directory in which to install the application. For example, from the root of your project, you may wish to put your application in a "js" or "javascripts" directory (creating the directory if it does not exist). Omitting the directory option will create the application in your current working directory. Here is an example of creating a ninjs application in a sub directory named "js" in the current working directory:
41
+ The first ninjs command you'll need is the "create" command. As it's name implies, this command creates a new ninjs application template, creating the necessary files and directories. The command accepts two arguments. The first is the name of your application object, which is required. The second argument, which is optional, is a directory in which to install the application. For example, from the root of your project, you may wish to put your application in a "js" or "javascripts" directory (creating the directory if it does not exist). Omitting the directory option will create the application in your current working directory. Here is an example of creating a ninjs application in a sub directory named "js" in the current working directory:
40
42
 
41
43
  ```sh
42
44
  ninjs create myapplication js
@@ -44,10 +46,10 @@ ninjs create myapplication js
44
46
 
45
47
  This will create a ninjs application in a directory named "js" in the current working directory. Now we can begin developing our application.
46
48
 
47
- Create a ninjs module
49
+ Creating a ninjs module
48
50
  =====================
49
51
 
50
- Using the generate command we can create a module scafoold. The generate command takes two arguments, the first is the type of file you'd like to generate (module, elements, or model) and the second is the name of the file/module the scaffold is for. Here's some examples of using the generate command with a module named hello:
52
+ Using the generate command we can create a module scaffold. The generate command accepts two arguments, the first is the type of file to generate (ie. module, elements, or model). The second is the name of the module. Here's some examples of using the generate command, creating a module named hello:
51
53
 
52
54
  ```sh
53
55
  ninjs generate module hello
@@ -55,57 +57,59 @@ ninjs generate elements hello
55
57
  ninjs generate model hello
56
58
  ```
57
59
 
58
- If you'd like to generate a module with elements and a model all in one go you can pass the -e and -m flags respectively:
60
+ If you'd like to generate a module, elements, and a model in one command you can pass the -e and -m flags respectively:
59
61
 
60
62
  ```sh
61
63
  ninjs generate module hello -em
62
64
  ````
63
65
 
64
- Although the generate method is very convenient, you may choose to create a module file manually in the /modules directory. By convention the file name ends with a suffix of .module. An example of a module named hello would look like this:
66
+ Although the generate method is very convenient, you may create a module file manually in the /modules directory. By convention the file name ends with a suffix of .module.js. An example of a module named hello would look like this:
65
67
 
66
68
  ```sh
67
69
  /modules/hello.module.js
68
70
  ```
69
71
 
70
- The basic functionality of a module is to encapsulate specific pieces of logic into a namespace. You may think of a module as a class in the sense that it allows you to namespace properties and behavior within your application. Typically, a module encapsulates the behavior on a specific page. A ninjs module is an extremely lightweight object with a simple API which helps you write clear, concise code. Here's what an empty module scaffold looks like (generated with ninjs generate model hello -em):
72
+ The basic functionality of a module is to encapsulate specific behavior in a namespace. You may think of a module as a class in the sense that it allows you to encapsulate properties and methods within your application. Typically, a module controls the behavior on a specific page or section. A module is an extremely lightweight object with a simple API to help you write clear, concise code. An empty module scaffold looks like the following (generated with ninjs generate model hello -em):
71
73
 
72
74
  ```js
73
75
  (function() {
74
- var self = myapplication.add_module('hello');
76
+ var mod = myapplication.add_module('hello');
75
77
 
76
78
  //= require "../models/hello.model"
77
79
  //= require "../elements/hello.elements"
78
80
 
79
- myapplication.hello.actions = function() {
81
+ mod.actions = function() {
80
82
 
81
83
  };
82
84
 
83
- myapplication.hello.run();
85
+ mod.run();
84
86
  })();
85
87
  ```
86
88
 
87
- Notice the module is wrapped in a closure. This allows us to make a private reference to the current module named "self" due to the fact that the add_module method returns the module it creates. This gives us a consistent way to reference the module without having to use the entire namespace. We may also decide to put private variables and functions available to the module but not exposed to the rest of the application.
89
+ Notice the module is wrapped in a closure. This allows us to make a private reference to the current module named "mod"* due to the fact that the add_module method returns the module it creates. This gives us a consistent way to reference the module without having to use the entire namespace. We may also decide to put private variables and functions available to the module but not exposed to the rest of the application or third party scripts. (*can be changed in the ninjs.conf).
88
90
 
89
- The actions method is the main method of your module. Also known as the composed method pattern, the actions method should simply be a list of other module methods. This makes it easy to scan the actions method to get a sense of what a given module does. It also encourages the "single responsibility principle" (http://en.wikipedia.org/wiki/Single_responsibility_principle).
91
+ The actions method is the main method of your module. The actions method is simply a list of module methods to call with the "run" method, this is known as the composed method pattern. This makes it easy to scan the actions method to get a sense of what a given module does. It also encourages the "single responsibility principle" (http://en.wikipedia.org/wiki/Single_responsibility_principle).
90
92
 
91
- The run method will execute the actions method when the DOM is ready to be manipulated. This is similar to jQuery's $(document).ready() method. If you wish to execute your modules actions as soon as the script is parsed you may call the execute method instead of run:
93
+ The "run" method will execute the actions method when the DOM is ready to be manipulated. This is the same thing as jQuery's $(document).ready() method. If you wish to execute your modules actions as soon as the script is parsed you may call the "execute" method instead of "run":
92
94
 
93
95
  ```js
94
- myapplication.hello.execute();
96
+ myapplication.hello.execute(); // executes immediately
95
97
  ```
96
98
 
97
- This pattern allows you to write in a literate style, making your intentions clear and methods succinct. It's idiomatic to use the module namespace (instead of using self) when defining your module methods to enhance clarity. Another advantage of wrapping your modules in a closure is that you may choose to define an alias for your application object which makes it easier to type while avoiding creation of another global variable. To do this, simply pass in the application object as an argument to the outer function and then name the alias in the argument to the closure like so:
99
+ This pattern allows you to write in a literate style, making your intentions clear and methods succinct. Another advantage of wrapping your modules in a closure is that you may choose to define an alias for your application object which makes it easier to access, while avoiding creation of a global variable. To do this, simply pass in the application object as an argument to the outer function and then name the alias in the argument to the closure like so:
98
100
 
99
101
  ```js
100
102
  (function(app) {
101
-
102
- var self = app.add_module('hello');
103
-
104
- app.hello.actions = function() {
103
+
104
+ var mod = app.add_module('hello');
105
105
 
106
+ mod.actions = function() {
107
+ app.global_method();
108
+ app.global_property = 'foo';
109
+ app.another_module.some_method();
106
110
  };
107
111
 
108
- app.hello.run();
112
+ mod.run();
109
113
 
110
114
  })(myapplication);
111
115
  ```
@@ -125,40 +129,42 @@ ninjs generate module hello -a myalias
125
129
  This will generate a file using an alias defined as "myalias". This is simply a convenience and a bit of syntactic sugar, although there are practical benefits to this as well. For example using this pattern, any module can be ported to another application simply by swapping out the application object passed to the closure.
126
130
 
127
131
  Let's take a closer look at the "actions" composed method pattern. Let's define a simple module which demonstrates this:
128
-
132
+
129
133
  ```js
130
- (function() {
131
- var self = myapplication.add_module('hello');
134
+ (function(app) {
135
+ var mod = app.add_module('hello');
132
136
 
133
- myapplication.hello.actions = function() {
134
- self.set_defaults();
135
- self.say_hello();
137
+ mod.actions = function() {
138
+ mod.set_defaults();
139
+ mod.say_hello();
136
140
  };
137
141
 
138
- myapplication.hello.set_defaults = function() {
139
- self.greeting = 'Hello';
140
- self.guest = 'World';
142
+ mod.set_defaults = function() {
143
+ mod.set_data({
144
+ greeting: 'Hello',
145
+ guest: 'World'
146
+ });
141
147
  };
142
148
 
143
- myapplication.hello.say_hello = function() {
144
- alert(self.get_greeting());
149
+ mod.say_hello = function() {
150
+ alert(mod.get_greeting());
145
151
  };
146
152
 
147
- myapplication.get_greeting = function() {
148
- return self.greeting + ' ' + self.guest + '!';
153
+ mod.get_greeting = function() {
154
+ return mod.data.greeting + ' ' + mod.data.guest + '!';
149
155
  };
150
156
 
151
- myapplication.hello.run();
152
- })();
157
+ mod.run();
158
+ })(myapplication);
153
159
  ```
154
160
 
155
161
  Although the complexity of this module is contrived, we can easily see what this module does simply by glancing at the actions method. This will make your application logic easier to write, understand, modify, and test
156
162
 
157
- Create module elements
163
+ Creating elements
158
164
  ======================
159
165
 
160
- Another common best practice that ninjs encourages is cacheing your element selectors. For example, when using jQuery to select a DOM element, it's typical to assign the result of the selection to a variable in case you need it again. Here's what this looks like in practice:
161
-
166
+ Another common best practice that ninjs encourages is cacheing your element queries. For example, when using jQuery to select a DOM element, it's typical to assign the result of the query to a variable in case you need it again. Here's what this looks like in practice:
167
+
162
168
  ```js
163
169
  // Bad no-caching
164
170
  $('#some-element').css({ 'background-color': '#FF0000' });
@@ -170,10 +176,10 @@ var some_element = $('#some-element');
170
176
  some_element.css({ 'background-color': '#FF0000' });
171
177
  some_element.html("I turned red");
172
178
  ```
173
-
179
+
174
180
  When we cache our selections, we only have to search the DOM once, improving performance.
175
181
 
176
- The only problem with this is that we tend to manipulate a lot of elements and our code can become littered with them. At worst, they're strewn about the file wherever they are used, making it easy to accidentally re-assign them. At best, all selections are cached in one place and easy to see, which prevents us from accidentally caching them twice but it becomes a fair amount of boilerplate that crufts up the file. ninjs goes a step further by putting these cached selectors in their own file in the elements folder. This gives us one place to manage all the cached selectors available to our module.
182
+ The only problem with this is that we tend to manipulate a lot of elements and our code can become littered with them. At worst, they're strewn about the file wherever they are used, making it easy to accidentally re-assign them. At best, all querys are assigned in one place and easy to see, which prevents us from accidentally caching them twice but it becomes a fair amount of boilerplate that clutter up the file. ninjs goes a step further by putting these cached selectors in their own file in the elements folder. This gives us one place to manage all the cached elements available to a module.
177
183
 
178
184
  Elements belong to a module and can be added using the elements method. To add elements to the hello module, let's add a hello.elements.js with the generate command:
179
185
 
@@ -181,64 +187,70 @@ Elements belong to a module and can be added using the elements method. To add e
181
187
  ninjs generate elements hello
182
188
  ```
183
189
 
184
- This will create a hello.elements.js file inside the elements directory. The elements scaffold looks like this:
190
+ This will create a hello.elements.js file inside the elements directory. The scaffold looks like this:
185
191
 
186
192
  ```js
187
- myapplication.hello.elements({
193
+ mod.dom.ready(function() {
194
+ mod.elements({
188
195
 
196
+ });
189
197
  });
190
198
  ```
191
199
 
192
- The elements method facilitates both setting and getting cached elements (violating the single responsibility principle for convenience). When passed an object, the elements method maps the key/value pairs of name/selector to the modules dom object. When passed a string, the elements method pulls the selector via it's name. To set a module's elements, pass an object of key value pairs like so:
200
+ The elements method facilitates both setting and getting cached elements. Notice that the elements method is wrapped in the module's dom.ready method to be sure the elements are available. When passed an object, the elements method maps the key/value pairs of name/selector to the modules dom object. When passed a string, the elements method accesses the selector via it's name. To set a module's elements, pass an object of key value pairs like so:
193
201
 
194
202
  ```js
195
- myapplication.hello.elements({
196
- message_box: $('#message-box')
203
+ mod.dom.ready(function() {
204
+ mod.elements({
205
+ message_box: $('#message-box')
206
+ });
197
207
  });
198
208
  ```
199
209
 
200
- Now these cached jQuery selectors will be available via the elements command by passing it's name into the method like so:
210
+ Now these cached querys will be available via the elements command by passing it's name into the method like so:
201
211
 
202
212
  ```js
203
213
  myapplication.hello.some_method = function() {
204
- self.elements('message_box');
214
+ mod.elements('message_box');
205
215
  };
206
216
  ```
207
217
 
208
- This pattern provides a consistent way to access and create elements and also creates an opportunity to add logic to the process (as in the ninjs.jquery.elements.js plugin). That's all there is to creating module elements. Now we require the file in the module using the "Sprockets require directive" (http://getsprockets.org/installation_and_usage#specifying_dependencies_with_the_require_directive) in hello.module.js.
209
-
218
+ This pattern provides a consistent way to access and create elements and also creates an opportunity to add logic to the process (as in the ninjs-jquery plugin). That's all there is to creating ninjs elements.
219
+
220
+ Now we need to include the file in the module using the "Sprockets require directive" (http://getsprockets.org/installation_and_usage#specifying_dependencies_with_the_require_directive) in hello.module.js.
221
+
210
222
  ```js
211
- (function() {
212
- var self = myapplication.add_module('hello');
223
+ (function(app) {
224
+ var mod = app.add_module('hello');
213
225
 
214
- //= require "../elements/hello.elements"
226
+ //= require "../elements/hello.elements"
215
227
 
216
- ...
217
- })();
228
+ ...
229
+ })(myapplication);
218
230
  ```
219
231
 
220
- Be sure to require the elements file after the "add_module" method is called. Now all elements defined in the elements method will be available to the module. Let's take our hello example and instead of alerting the greeting, let's put it in the message_box element (assuming an html page containing this element):
221
-
232
+ Be sure to require the elements file after the "add_module" method is called. Now all elements assigned in the elements file will be available to the module in our compiled application. Let's take our hello example and refactor it a little. Instead of alerting the greeting, let's put it in the message_box element (assuming an html page containing this element):
233
+
222
234
  ```js
223
235
  ...
224
236
 
225
- myapplication.hello.say_hello = function() {
226
- self.elements('message_box').html(self.get_greeting());
237
+ mod.say_hello = function() {
238
+ mod.elements('message_box').html(mod.get_greeting());
227
239
  };
228
240
 
229
241
  ...
230
242
  ```
231
-
232
- Again, this pattern keeps the logic very clear and our code very concise. It's easy to read, test, and refactor. With time, you'll develop your own naming conventions and standards. The important thing is to focus on good semantic names that accurately describe the properties and behavior of your application.
243
+
244
+ Again, this pattern keeps the logic very clear and our code very concise. It's easy to read, test, and refactor. The important thing is to focus on good semantic names that accurately describe the properties and behavior of your module.
233
245
 
234
246
  Most modules will be exactly like the one we just created, only with more methods. However, there is one more piece that helps you achieve greater modularity, which is ninjs models.
235
247
 
236
- Create a ninjs model
248
+ Creating a ninjs model
237
249
  ====================
238
250
 
239
- ninjs models are simply files in the /models directory that define a data structure for the module. Every module has a data object that stores properties for reuse or configuration.
251
+ ninjs models are simply files in the /models directory that define a data object for the module. Every module has a data object that stores properties for reuse or configuration.
240
252
 
241
- Let's suppose I have a plugin that I want to use in my module. The plugin takes a configuration object, but I want to be able to set a default configuration for my module. I'll use the generate command to create a model:
253
+ Let's suppose I have a plugin that I want to use in my module. The plugin takes a configuration object, but I want to be able to set a default configuration for my module. First I use the generate command to create a model file:
242
254
 
243
255
  ```sh
244
256
  ninjs generate model hello
@@ -247,17 +259,17 @@ ninjs generate model hello
247
259
  This will create an empty model scaffold that looks like this:
248
260
 
249
261
  ```js
250
- myapplication.hello.set_data({
262
+ mod.set_data({
251
263
 
252
264
  });
253
265
  ```
254
266
 
255
- The set_data method takes either an object or a string as the first argument. When an object is used as the first argument, the key value pairs contained in the object will be copied to the module's "data" object. When a string is used, the first argument is a string representing the name of the property to assign and the second is the value to assign to that property. Using the string method, we could define a configuration object for the plugin like so:
267
+ The set_data method takes either an object or a string as the first argument. When an object is used as the first argument, the key value pairs contained in the object will be copied to the module's "data" object. When passed a string, the first argument is used as the name of the property and the second is the value. We could define a configuration object for the plugin like so:
256
268
 
257
269
  ```js
258
- myapplication.hello.set_data('plugin_config', {
259
- width: 300,
260
- height: 250
270
+ mod.set_data('plugin_config', {
271
+ width: 300,
272
+ height: 250
261
273
  });
262
274
  ```
263
275
 
@@ -265,25 +277,25 @@ If we wish to set several properties at once we can use just an object as the fi
265
277
 
266
278
  ```js
267
279
  myapplication.hello.set_data({
268
- plugin_config: {
269
- width: 300,
270
- height: 250
271
- },
272
- another_property: 'some value'
280
+ plugin_config: {
281
+ width: 300,
282
+ height: 250
283
+ },
284
+ another_property: 'some value'
273
285
  });
274
286
  ```
275
287
 
276
288
  Next we include the model in the module file:
277
289
 
278
290
  ```js
279
- (function() {
280
- var self = myapplication.add_module('hello');
291
+ (function(app) {
292
+ var mod = app.add_module('hello');
281
293
 
282
- //= require "../elements/hello.model.js"
283
- //= require "../elements/hello.elements"
294
+ //= require "../elements/hello.model.js"
295
+ //= require "../elements/hello.elements"
284
296
 
285
- ...
286
- })();
297
+ ...
298
+ })(myapplication);
287
299
  ```
288
300
 
289
301
  Now whenever I use the plugin in my module, I can mix in the config object like so:
@@ -291,25 +303,25 @@ Now whenever I use the plugin in my module, I can mix in the config object like
291
303
  ```js
292
304
  ...
293
305
 
294
- myapplication.hello.setup_plugin = function() {
295
- self.some_element.some_plugin(self.data.plugin_config);
296
- self.another_element.some_plugin(self.data.plugin_config);
306
+ mod.setup_plugin = function() {
307
+ mod.some_element.some_plugin(mod.data.plugin_config);
308
+ mod.another_element.some_plugin(mod.data.plugin_config);
297
309
  };
298
310
 
299
311
  ...
300
312
  ```
301
313
 
302
- This way, we don't have to keep redefining the same properties each time we use this plugin. If we want to extend the defaults, we can use something like jQuery's extend method:
314
+ This way, we don't have to keep redefining the same properties, drying up our code. If we want to extend the defaults, we can use something like jQuery's extend method:
303
315
 
304
316
  ```js
305
317
  ...
306
318
 
307
- myapplication.hello.setup_plugin = function() {
308
- self.some_element.some_plugin(self.data.plugin_config);
309
- self.another_element.some_plugin($.extend(self.data.plugin_config, {
310
- height: 300,
311
- color: #FF0000
312
- }));
319
+ mod.setup_plugin = function() {
320
+ mod.some_element.some_plugin(mod.data.plugin_config);
321
+ mod.another_element.some_plugin($.extend(mod.data.plugin_config, {
322
+ height: 300,
323
+ color: #FF0000
324
+ }));
313
325
  };
314
326
 
315
327
  ...
@@ -320,13 +332,15 @@ The model provides a foundation we can build from, helping use to keep our code
320
332
  Compiling the application
321
333
  =========================
322
334
 
323
- Now that we have a complete module including elements and a model, we need to compile these into one coherent file to use in our html. To do so, we have 2 options. Open up a terminal and navigate to the root of your ninjs application. We can compile our application with one of 2 commands. The first choice is the compile command. From the root of your ninjs application type:
335
+ Now that we have a complete module with elements and a model, we need to compile these into one coherent file to use in our html. To do so, we have 2 options. Open up a terminal and navigate to the root of your ninjs application. We can compile our application with one of 2 commands. The first choice is the compile command. From the root of your ninjs application type:
324
336
 
325
337
  ```sh
326
338
  ninjs compile
327
339
  ```
328
340
 
329
- This will compile all the files in the modules folder, resolving all dependencies using the Sprockets engine, and finally outputting them into the /application directory with the .module suffix removed. Our hello example module would compile into the application folder as hello.js. Now we can include the hello.js file (along with the myapplication.js) in our html document. Since running compile every time we make a change to any one of our module source files would quickly become a tedious chore, ninjs also has a watch command which will watch your project directory for changes and automatically compile when a file is changed. This speeds up development considerably and frankly makes ninjs usable in a daily development context. To watch a ninjs project simply navigate to the project root and issue the watch command:
341
+ This will compile all the files in the modules (src_dir) folder, resolving all dependencies using the Sprockets engine, and finally outputting them into the /application directory with the .module suffix removed. Our hello example module would compile into the application folder named hello.js. Now we can include the hello.js file (along with the myapplication.js) in our html document.
342
+
343
+ Running compile every time we make a change to any one of our source files would quickly become a tedious chore, ninjs has a watch command that will automatically compile when a file is changed. This speeds up development considerably and frankly makes it usable in a daily development context. To watch a ninjs project simply navigate to the project root and issue the watch command:
330
344
 
331
345
  ```sh
332
346
  ninjs watch
@@ -340,6 +354,7 @@ That's it! you've created a basic ninjs application! The only step left is to in
340
354
  <title>ninjs Demo</title>
341
355
  </head>
342
356
  <body>
357
+ <div id="message-box"></div>
343
358
  <script src="js/application/myapplication.js"></script>
344
359
  <script src="js/application/hello.js"></script>
345
360
  </body>