ninjs 0.14.1 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.bundle/config +2 -2
- data/.travis.yml +1 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +13 -11
- data/README.md +108 -93
- data/Rakefile +17 -7
- data/VERSION +1 -1
- data/bin/ninjs +24 -22
- data/lib/ninjs/command.rb +32 -21
- data/lib/ninjs/configuration.rb +35 -53
- data/lib/ninjs/generator.rb +6 -6
- data/lib/ninjs/notification.rb +5 -5
- data/lib/ninjs/project.rb +31 -25
- data/ninjs.gemspec +18 -142
- data/repository/ninjs/core/application.js +13 -24
- data/repository/ninjs/core/dom.js +87 -68
- data/repository/ninjs/core/existence.js +44 -105
- data/repository/ninjs/core/extend.js +11 -32
- data/repository/ninjs/core/module.js +53 -107
- data/repository/ninjs/core/nin.js +5 -7
- data/repository/ninjs/tests/extension.test.js +0 -10
- data/repository/ninjs/tests/index.html +19 -20
- data/repository/ninjs/tests/module.test.js +52 -56
- data/repository/ninjs/utilities/array.js +8 -8
- data/repository/ninjs/utilities/cookie.js +59 -0
- data/repository/ninjs/utilities/css.js +7 -7
- data/repository/ninjs/utilities/number.js +2 -2
- data/repository/ninjs/utilities/string.js +26 -26
- data/spec/cli_spec.rb +90 -88
- data/spec/command_spec.rb +104 -137
- data/spec/configuration_spec.rb +23 -37
- data/spec/fixtures/_global.module.js +9 -0
- data/spec/fixtures/compressed.myapp.js +23 -44
- data/spec/fixtures/foo.elements.js +1 -1
- data/spec/fixtures/foo.js +5 -5
- data/spec/fixtures/foo.model.js +1 -1
- data/spec/fixtures/foo.module.js +3 -3
- data/spec/fixtures/hello.elements.js +1 -1
- data/spec/fixtures/hello.js +5 -5
- data/spec/fixtures/hello.model.js +1 -1
- data/spec/fixtures/hello.module.js +3 -3
- data/spec/fixtures/myapp.initial.js +198 -329
- data/spec/fixtures/myapp.js +285 -357
- data/spec/fixtures/mymodule.alias.module.js +2 -2
- data/spec/fixtures/mymodule.dependencies.module.js +2 -2
- data/spec/fixtures/mymodule.elements.js +1 -1
- data/spec/fixtures/mymodule.model.js +1 -1
- data/spec/fixtures/mymodule.module.js +2 -2
- data/spec/fixtures/mymodule.module_alias.module.js +10 -0
- data/spec/fixtures/new.ninjs.conf +2 -0
- data/spec/fixtures/nin.js +198 -329
- data/spec/fixtures/ninjs.conf +2 -0
- data/spec/fixtures/updated.ninjs.conf +8 -0
- data/spec/generator_spec.rb +37 -26
- data/spec/ninjs_spec.rb +6 -6
- data/spec/project_spec.rb +229 -221
- data/spec/spec_helper.rb +3 -1
- data/spec/testspec_spec.rb +7 -0
- metadata +28 -145
- data/repository/json2.js +0 -482
- data/repository/modernizr/1.5.js +0 -28
- data/repository/modernizr/latest.js +0 -1
- data/repository/ninjs/docs/Data/ClassHierarchy.nd +0 -0
- data/repository/ninjs/docs/Data/ConfigFileInfo.nd +0 -0
- data/repository/ninjs/docs/Data/FileInfo.nd +0 -16
- data/repository/ninjs/docs/Data/ImageFileInfo.nd +0 -0
- data/repository/ninjs/docs/Data/ImageReferenceTable.nd +0 -0
- data/repository/ninjs/docs/Data/IndexInfo.nd +0 -0
- data/repository/ninjs/docs/Data/PreviousMenuState.nd +0 -0
- data/repository/ninjs/docs/Data/PreviousSettings.nd +0 -0
- data/repository/ninjs/docs/Data/SymbolTable.nd +0 -0
- data/repository/ninjs/docs/Languages.txt +0 -113
- data/repository/ninjs/docs/Menu.txt +0 -63
- data/repository/ninjs/docs/Topics.txt +0 -81
- data/repository/ninjs/docs/files/core/application-js.html +0 -44
- data/repository/ninjs/docs/files/core/existence-js.html +0 -60
- data/repository/ninjs/docs/files/core/extend-js.html +0 -55
- data/repository/ninjs/docs/files/core/module-js.html +0 -80
- data/repository/ninjs/docs/files/core/nin-js.html +0 -38
- data/repository/ninjs/docs/index.html +0 -1
- data/repository/ninjs/docs/index/Classes.html +0 -33
- data/repository/ninjs/docs/index/Files.html +0 -45
- data/repository/ninjs/docs/index/Functions.html +0 -65
- data/repository/ninjs/docs/index/General.html +0 -85
- data/repository/ninjs/docs/index/Variables.html +0 -45
- data/repository/ninjs/docs/javascript/main.js +0 -841
- data/repository/ninjs/docs/javascript/prettify.js +0 -1526
- data/repository/ninjs/docs/javascript/searchdata.js +0 -152
- data/repository/ninjs/docs/search/ClassesN.html +0 -20
- data/repository/ninjs/docs/search/FilesA.html +0 -20
- data/repository/ninjs/docs/search/FilesE.html +0 -20
- data/repository/ninjs/docs/search/FilesM.html +0 -20
- data/repository/ninjs/docs/search/FilesN.html +0 -20
- data/repository/ninjs/docs/search/FunctionsA.html +0 -20
- data/repository/ninjs/docs/search/FunctionsC.html +0 -20
- data/repository/ninjs/docs/search/FunctionsE.html +0 -20
- data/repository/ninjs/docs/search/FunctionsI.html +0 -20
- data/repository/ninjs/docs/search/FunctionsM.html +0 -20
- data/repository/ninjs/docs/search/FunctionsR.html +0 -20
- data/repository/ninjs/docs/search/FunctionsS.html +0 -20
- data/repository/ninjs/docs/search/FunctionsSymbols.html +0 -20
- data/repository/ninjs/docs/search/FunctionsU.html +0 -20
- data/repository/ninjs/docs/search/GeneralA.html +0 -20
- data/repository/ninjs/docs/search/GeneralC.html +0 -20
- data/repository/ninjs/docs/search/GeneralD.html +0 -20
- data/repository/ninjs/docs/search/GeneralE.html +0 -20
- data/repository/ninjs/docs/search/GeneralF.html +0 -20
- data/repository/ninjs/docs/search/GeneralI.html +0 -20
- data/repository/ninjs/docs/search/GeneralM.html +0 -20
- data/repository/ninjs/docs/search/GeneralN.html +0 -20
- data/repository/ninjs/docs/search/GeneralR.html +0 -20
- data/repository/ninjs/docs/search/GeneralS.html +0 -20
- data/repository/ninjs/docs/search/GeneralSymbols.html +0 -20
- data/repository/ninjs/docs/search/GeneralT.html +0 -20
- data/repository/ninjs/docs/search/GeneralU.html +0 -20
- data/repository/ninjs/docs/search/GeneralV.html +0 -20
- data/repository/ninjs/docs/search/NoResults.html +0 -15
- data/repository/ninjs/docs/search/VariablesD.html +0 -20
- data/repository/ninjs/docs/search/VariablesN.html +0 -20
- data/repository/ninjs/docs/search/VariablesR.html +0 -20
- data/repository/ninjs/docs/search/VariablesT.html +0 -20
- data/repository/ninjs/docs/styles/main.css +0 -828
- data/repository/nullconsole.js +0 -7
- data/repository/qunit/assets/css/qunit.css +0 -196
- data/repository/qunit/qunit.js +0 -1374
- data/repository/selectivizr/1.0.js +0 -5
- data/repository/selectivizr/latest.js +0 -1
- data/repository/syntaxhighlighter/all.js +0 -11
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCore.css +0 -226
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreDefault.css +0 -328
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreDjango.css +0 -331
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreEclipse.css +0 -339
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreEmacs.css +0 -324
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreMDUltra.css +0 -324
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreMidnight.css +0 -324
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shCoreRDark.css +0 -324
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeDefault.css +0 -117
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeDjango.css +0 -120
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeEclipse.css +0 -128
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeEmacs.css +0 -113
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeMDUltra.css +0 -113
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeMidnight.css +0 -113
- data/repository/syntaxhighlighter/assets/css/syntaxhighlighter/shThemeRDark.css +0 -113
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCore.scss +0 -216
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreDefault.scss +0 -2
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreDjango.scss +0 -2
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreEclipse.scss +0 -2
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreEmacs.scss +0 -2
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreFadeToGrey.scss +0 -2
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreMDUltra.scss +0 -2
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreMidnight.scss +0 -2
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shCoreRDark.scss +0 -2
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeDefault.scss +0 -7
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeDjango.scss +0 -36
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeEclipse.scss +0 -48
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeEmacs.scss +0 -32
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeFadeToGrey.scss +0 -36
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeMDUltra.scss +0 -32
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeMidnight.scss +0 -32
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_shThemeRDark.scss +0 -32
- data/repository/syntaxhighlighter/assets/scss/syntaxhighlighter/_theme_template.scss +0 -120
- data/repository/syntaxhighlighter/shAutoloader.js +0 -17
- data/repository/syntaxhighlighter/shBrushAS3.js +0 -59
- data/repository/syntaxhighlighter/shBrushAppleScript.js +0 -75
- data/repository/syntaxhighlighter/shBrushBash.js +0 -59
- data/repository/syntaxhighlighter/shBrushCSharp.js +0 -65
- data/repository/syntaxhighlighter/shBrushColdFusion.js +0 -100
- data/repository/syntaxhighlighter/shBrushCpp.js +0 -97
- data/repository/syntaxhighlighter/shBrushCss.js +0 -91
- data/repository/syntaxhighlighter/shBrushDelphi.js +0 -55
- data/repository/syntaxhighlighter/shBrushDiff.js +0 -41
- data/repository/syntaxhighlighter/shBrushErlang.js +0 -52
- data/repository/syntaxhighlighter/shBrushGroovy.js +0 -67
- data/repository/syntaxhighlighter/shBrushJScript.js +0 -52
- data/repository/syntaxhighlighter/shBrushJava.js +0 -57
- data/repository/syntaxhighlighter/shBrushJavaFX.js +0 -58
- data/repository/syntaxhighlighter/shBrushPerl.js +0 -72
- data/repository/syntaxhighlighter/shBrushPhp.js +0 -88
- data/repository/syntaxhighlighter/shBrushPlain.js +0 -33
- data/repository/syntaxhighlighter/shBrushPowerShell.js +0 -74
- data/repository/syntaxhighlighter/shBrushPython.js +0 -64
- data/repository/syntaxhighlighter/shBrushRuby.js +0 -55
- data/repository/syntaxhighlighter/shBrushSass.js +0 -94
- data/repository/syntaxhighlighter/shBrushScala.js +0 -51
- data/repository/syntaxhighlighter/shBrushSql.js +0 -66
- data/repository/syntaxhighlighter/shBrushVb.js +0 -56
- data/repository/syntaxhighlighter/shBrushXml.js +0 -69
- data/repository/syntaxhighlighter/shCore.js +0 -17
- data/repository/syntaxhighlighter/shLegacy.js +0 -17
data/.bundle/config
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
---
|
2
|
-
|
1
|
+
--- {}
|
2
|
+
|
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm: 1.9.2
|
data/Gemfile
CHANGED
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.
|
5
|
-
fssm (0.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.
|
13
|
-
rcov (0.9.
|
14
|
-
|
15
|
-
|
16
|
-
rspec-
|
17
|
-
rspec-
|
18
|
-
|
19
|
-
rspec-
|
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.
|
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
|
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/
|
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
|
-
|
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
|
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
|
-
|
49
|
+
Creating a ninjs module
|
48
50
|
=====================
|
49
51
|
|
50
|
-
Using the generate command we can create a module
|
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
|
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
|
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
|
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
|
76
|
+
var mod = myapplication.add_module('hello');
|
75
77
|
|
76
78
|
//= require "../models/hello.model"
|
77
79
|
//= require "../elements/hello.elements"
|
78
80
|
|
79
|
-
|
81
|
+
mod.actions = function() {
|
80
82
|
|
81
83
|
};
|
82
84
|
|
83
|
-
|
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 "
|
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.
|
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
|
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.
|
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
|
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
|
-
|
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
|
134
|
+
(function(app) {
|
135
|
+
var mod = app.add_module('hello');
|
132
136
|
|
133
|
-
|
134
|
-
|
135
|
-
|
137
|
+
mod.actions = function() {
|
138
|
+
mod.set_defaults();
|
139
|
+
mod.say_hello();
|
136
140
|
};
|
137
141
|
|
138
|
-
|
139
|
-
|
140
|
-
|
142
|
+
mod.set_defaults = function() {
|
143
|
+
mod.set_data({
|
144
|
+
greeting: 'Hello',
|
145
|
+
guest: 'World'
|
146
|
+
});
|
141
147
|
};
|
142
148
|
|
143
|
-
|
144
|
-
alert(
|
149
|
+
mod.say_hello = function() {
|
150
|
+
alert(mod.get_greeting());
|
145
151
|
};
|
146
152
|
|
147
|
-
|
148
|
-
return
|
153
|
+
mod.get_greeting = function() {
|
154
|
+
return mod.data.greeting + ' ' + mod.data.guest + '!';
|
149
155
|
};
|
150
156
|
|
151
|
-
|
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
|
-
|
163
|
+
Creating elements
|
158
164
|
======================
|
159
165
|
|
160
|
-
Another common best practice that ninjs encourages is cacheing your element
|
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
|
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
|
190
|
+
This will create a hello.elements.js file inside the elements directory. The scaffold looks like this:
|
185
191
|
|
186
192
|
```js
|
187
|
-
|
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
|
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
|
-
|
196
|
-
|
203
|
+
mod.dom.ready(function() {
|
204
|
+
mod.elements({
|
205
|
+
message_box: $('#message-box')
|
206
|
+
});
|
197
207
|
});
|
198
208
|
```
|
199
209
|
|
200
|
-
Now these cached
|
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
|
-
|
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
|
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
|
-
|
223
|
+
(function(app) {
|
224
|
+
var mod = app.add_module('hello');
|
213
225
|
|
214
|
-
|
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
|
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
|
-
|
226
|
-
|
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.
|
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
|
-
|
248
|
+
Creating a ninjs model
|
237
249
|
====================
|
238
250
|
|
239
|
-
ninjs models are simply files in the /models directory that define a data
|
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
|
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
|
-
|
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
|
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
|
-
|
259
|
-
|
260
|
-
|
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
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
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
|
-
|
291
|
+
(function(app) {
|
292
|
+
var mod = app.add_module('hello');
|
281
293
|
|
282
|
-
|
283
|
-
|
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
|
-
|
295
|
-
|
296
|
-
|
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
|
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
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
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
|
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
|
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>
|