ninjs 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -48,23 +48,23 @@ The run method will execute the actions method. Please note that the "run" metho
48
48
  myapplication.hello.execute();
49
49
  </pre>
50
50
 
51
- This pattern allows you to write in a literate style while making your intentions clear and methods succinct. However, if you prefer a shorter syntax or make your module completely protable (transplant to any application), Ninjs defines two aliases to work with your application and modules that are more terse. Your application object will be aliased as "_" (underscore). When using either the run or execute methods to call the module's actions, the module will be available through the "__" alias. The previous hello module would look like the following:
51
+ This pattern allows you to write in a literate style while making your intentions clear and methods succinct. However, if you prefer a shorter syntax or make your module completely protable (transplant to any application), Ninjs defines an application alias to make your module code cleaner and your modules portable. Your application object will be aliased as "app". With the app alias we can write the previous module like so:
52
52
 
53
53
  <pre name="code" class="brush: js;">
54
- _.add_module('hello');
54
+ app.add_module('hello');
55
55
 
56
- _.hello.actions = function() {
57
- __.say_hello();
56
+ app.hello.actions = function() {
57
+ this.say_hello();
58
58
  };
59
59
 
60
- _.hello.say_hello = function() {
60
+ app.hello.say_hello = function() {
61
61
  alert('Hello World');
62
62
  };
63
63
 
64
- _.hello.run();
64
+ app.hello.run();
65
65
  </pre>
66
66
 
67
- This not only makes the module less cumbersome to write, it also allows you to port modules directly into other Ninjs application's and run them without any renaming. Remember one underscore is a reference to the application and two underscores is a reference to the current module.
67
+ This not only makes the module less cumbersome to write, it also allows you to port modules directly into other Ninjs application's and run them without any renaming.
68
68
 
69
69
  You may ask why we are calling say_hello() in the actions method instead of just alerting the string in actions itself. Let's set aside the fact that this is a trivial example and assume we will be adding many more methods to the module. If we simply added all of our module code inside actions, we'd quickly have a soup of code in there which would be difficult to follow. The Ninjs javascript framework encourages syntactic clarity. It's preferable for the actions method to be a list of methods called in the module. This let's my module tell a consistent story from top to bottom. The actions method serves as a table of contents. Consider a slightly more sophisticated hello module:
70
70
 
@@ -203,18 +203,65 @@ This way we don't have to keep redefining the same properties each time we call
203
203
  }
204
204
  </pre>
205
205
 
206
- The model provides a default base that we can build from, helping use to keep our code DRY. There's also an opportunity here to illustrate the utility of the "ninja" aliases. Notice that inside the dialog function we are unable to use the this.dialog_settings shorthand. Using "this" in Javascript can be a bit tricky. In this case, "this" is referring to the dialog and not the module. That's why Ninjs defines the double underscore ( __ ) variable to refer to the current module. The previous example can be rewritten more succinctly like so:
206
+ The model provides a default base that we can build from, helping use to keep our code DRY.
207
+
208
+
209
+ h1. Reference and Style
210
+
211
+ Notice the "this" variable used in the module methods. In this context "this" refers to the module itself. You need to be careful here because "this" in javascript is a fickle thing. Once you're inside another function, "this" no longer refers to the module, it refers to the current function. We need a way to refer to the modules without using the full namespace to access our module but that is quite a mouthful and tends to clutter up the methods, making them harder to read. The best way to get a local, private alias to our module is to wrap it in a closure like so:
207
212
 
208
213
  <pre name="code" class="brush: js;">
209
- MyApplciation.hello.create_dialogs = function() {
210
- __.error_dialog.dialog(__.dialog_settings);
211
- __.notice_dialog.dialog($.extend(__.dialog_settings, {
212
- height: 300,
213
- autoOpen: true
214
- }));
215
- }
214
+ (function(){
215
+ var self = myapplication.add_module('mymodule);
216
+
217
+ myapplication.mymodule.actions = function() {
218
+ self.observe_some_element_click();
219
+ };
220
+
221
+ myapplication.mymodule.observe_some_element_click = function() {
222
+ self.some_element.click(function() {
223
+ self.make_something_happen();
224
+ });
225
+ };
226
+
227
+ myapplication.mymodule.make_something_happen = function() {
228
+ alert('something happened');
229
+ };
230
+
231
+ ...
232
+ })();
216
233
  </pre>
217
234
 
235
+ Because the add_module method returns the module it just created, we can assign it to "self". This gives us a clear, consistent way to refer to the module. Although the framework is flexible in how you access your application and module objects, my favorite way to define modules looks something like this:
236
+
237
+ <pre name="code" class="brush: js;">
238
+ (function(){
239
+ var self = app.add_module('mymodule);
240
+
241
+ app.mymodule.actions = function() {
242
+ app.setup_some_plugin();
243
+ };
244
+
245
+ app.mymodule.setup_some_plugin = function() {
246
+ self.some_element.some_plugin(self.data.some_plugin_config);
247
+ };
248
+
249
+ app.mymodule.observe_some_element_click = function() {
250
+ self.some_element.click(function() {
251
+ self.make_something_happen();
252
+ });
253
+ };
254
+
255
+ app.mymodule.make_something_happen = function() {
256
+ alert('something happened');
257
+ };
258
+
259
+ app.mymodule.run();
260
+ })();
261
+ </pre>
262
+
263
+ Using the app alias makes your module copy-paste compatible with other ninjs applications, and it also cuts down on the typing. I like to define the methods using the app.module namespace because it helps me remember where I am when I'm in the middle of the file. Inside the methods I only refer to the module as self to be clear.
264
+
218
265
  h1. Compiling the application
219
266
 
220
267
  Now that we have a complete module including elements and a model, we need to compile these files into one coherent file to use in our html. To do so we have 2 options. Open a terminal window (command prompt) 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:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.12.0
1
+ 0.12.1
data/ninjs.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ninjs}
8
- s.version = "0.12.0"
8
+ s.version = "0.12.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dayton Nolan"]
12
- s.date = %q{2011-04-07}
12
+ s.date = %q{2011-04-08}
13
13
  s.default_executable = %q{ninjs}
14
14
  s.description = %q{Ninjs is a ruby application and small javascript framework that helps you build clean, modular javascript applications. Ninjs encourages "Good Parts" best practices and the Crockford school Module pattern (http://www.crockford.com/). The ninjs command line application is an automatic compiler, written in ruby, and based on the Sprockets library (http://getsprockets.org/).}
15
15
  s.email = %q{daytonn@gmail.com}
@@ -310,7 +310,7 @@ Gem::Specification.new do |s|
310
310
  s.licenses = ["MIT"]
311
311
  s.require_paths = ["lib"]
312
312
  s.rubyforge_project = %q{nowarning}
313
- s.rubygems_version = %q{1.3.7}
313
+ s.rubygems_version = %q{1.5.2}
314
314
  s.summary = %q{ninjs is a command line application to help you write clean, modular javascript applications.}
315
315
  s.test_files = [
316
316
  "spec/ninjs_spec.rb",
@@ -318,7 +318,6 @@ Gem::Specification.new do |s|
318
318
  ]
319
319
 
320
320
  if s.respond_to? :specification_version then
321
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
322
321
  s.specification_version = 3
323
322
 
324
323
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -42,7 +42,12 @@ NinjsApplication.method('add_module', function(name) {
42
42
  if (is_defined(this[name])) {
43
43
  throw new SyntaxError("NinjsApplication.add_module(name): '" + name + "' already declared");
44
44
  }
45
- this[name] = new NinjsModule(name);
45
+
46
+ if (this.name === name) {
47
+ throw new SyntaxError("NinjsApplication.add_module(name): a module cannot have the same name as the application");
48
+ }
49
+
50
+ return this[name] = new NinjsModule(name);
46
51
  }
47
52
  catch(error) {
48
53
  alert(error.message);
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ninjs
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 45
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 12
8
- - 0
9
- version: 0.12.0
9
+ - 1
10
+ version: 0.12.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Dayton Nolan
@@ -14,208 +15,223 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-04-07 00:00:00 -05:00
18
+ date: 2011-04-08 00:00:00 -05:00
18
19
  default_executable: ninjs
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
- name: rubikon
22
+ type: :runtime
22
23
  requirement: &id001 !ruby/object:Gem::Requirement
23
24
  none: false
24
25
  requirements:
25
26
  - - ">="
26
27
  - !ruby/object:Gem::Version
28
+ hash: 3
27
29
  segments:
28
30
  - 0
29
31
  version: "0"
30
- type: :runtime
31
- prerelease: false
32
+ name: rubikon
32
33
  version_requirements: *id001
34
+ prerelease: false
33
35
  - !ruby/object:Gem::Dependency
34
- name: fssm
36
+ type: :runtime
35
37
  requirement: &id002 !ruby/object:Gem::Requirement
36
38
  none: false
37
39
  requirements:
38
40
  - - ">="
39
41
  - !ruby/object:Gem::Version
42
+ hash: 3
40
43
  segments:
41
44
  - 0
42
45
  version: "0"
43
- type: :runtime
44
- prerelease: false
46
+ name: fssm
45
47
  version_requirements: *id002
48
+ prerelease: false
46
49
  - !ruby/object:Gem::Dependency
47
- name: jsmin
50
+ type: :runtime
48
51
  requirement: &id003 !ruby/object:Gem::Requirement
49
52
  none: false
50
53
  requirements:
51
54
  - - ">="
52
55
  - !ruby/object:Gem::Version
56
+ hash: 3
53
57
  segments:
54
58
  - 0
55
59
  version: "0"
56
- type: :runtime
57
- prerelease: false
60
+ name: jsmin
58
61
  version_requirements: *id003
62
+ prerelease: false
59
63
  - !ruby/object:Gem::Dependency
60
- name: sprockets
64
+ type: :runtime
61
65
  requirement: &id004 !ruby/object:Gem::Requirement
62
66
  none: false
63
67
  requirements:
64
68
  - - ">="
65
69
  - !ruby/object:Gem::Version
70
+ hash: 3
66
71
  segments:
67
72
  - 0
68
73
  version: "0"
69
- type: :runtime
70
- prerelease: false
74
+ name: sprockets
71
75
  version_requirements: *id004
76
+ prerelease: false
72
77
  - !ruby/object:Gem::Dependency
73
- name: metric_fu
78
+ type: :development
74
79
  requirement: &id005 !ruby/object:Gem::Requirement
75
80
  none: false
76
81
  requirements:
77
82
  - - ">="
78
83
  - !ruby/object:Gem::Version
84
+ hash: 3
79
85
  segments:
80
86
  - 0
81
87
  version: "0"
82
- type: :development
83
- prerelease: false
88
+ name: metric_fu
84
89
  version_requirements: *id005
90
+ prerelease: false
85
91
  - !ruby/object:Gem::Dependency
86
- name: shoulda
92
+ type: :development
87
93
  requirement: &id006 !ruby/object:Gem::Requirement
88
94
  none: false
89
95
  requirements:
90
96
  - - ">="
91
97
  - !ruby/object:Gem::Version
98
+ hash: 3
92
99
  segments:
93
100
  - 0
94
101
  version: "0"
95
- type: :development
96
- prerelease: false
102
+ name: shoulda
97
103
  version_requirements: *id006
104
+ prerelease: false
98
105
  - !ruby/object:Gem::Dependency
99
- name: bundler
106
+ type: :development
100
107
  requirement: &id007 !ruby/object:Gem::Requirement
101
108
  none: false
102
109
  requirements:
103
110
  - - ~>
104
111
  - !ruby/object:Gem::Version
112
+ hash: 23
105
113
  segments:
106
114
  - 1
107
115
  - 0
108
116
  - 0
109
117
  version: 1.0.0
110
- type: :development
111
- prerelease: false
118
+ name: bundler
112
119
  version_requirements: *id007
120
+ prerelease: false
113
121
  - !ruby/object:Gem::Dependency
114
- name: jeweler
122
+ type: :development
115
123
  requirement: &id008 !ruby/object:Gem::Requirement
116
124
  none: false
117
125
  requirements:
118
126
  - - ~>
119
127
  - !ruby/object:Gem::Version
128
+ hash: 7
120
129
  segments:
121
130
  - 1
122
131
  - 5
123
132
  - 2
124
133
  version: 1.5.2
125
- type: :development
126
- prerelease: false
134
+ name: jeweler
127
135
  version_requirements: *id008
136
+ prerelease: false
128
137
  - !ruby/object:Gem::Dependency
129
- name: rcov
138
+ type: :development
130
139
  requirement: &id009 !ruby/object:Gem::Requirement
131
140
  none: false
132
141
  requirements:
133
142
  - - ">="
134
143
  - !ruby/object:Gem::Version
144
+ hash: 3
135
145
  segments:
136
146
  - 0
137
147
  version: "0"
138
- type: :development
139
- prerelease: false
148
+ name: rcov
140
149
  version_requirements: *id009
150
+ prerelease: false
141
151
  - !ruby/object:Gem::Dependency
142
- name: rspec
152
+ type: :development
143
153
  requirement: &id010 !ruby/object:Gem::Requirement
144
154
  none: false
145
155
  requirements:
146
156
  - - ">="
147
157
  - !ruby/object:Gem::Version
158
+ hash: 3
148
159
  segments:
149
160
  - 0
150
161
  version: "0"
151
- type: :development
152
- prerelease: false
162
+ name: rspec
153
163
  version_requirements: *id010
164
+ prerelease: false
154
165
  - !ruby/object:Gem::Dependency
155
- name: rubikon
166
+ type: :runtime
156
167
  requirement: &id011 !ruby/object:Gem::Requirement
157
168
  none: false
158
169
  requirements:
159
170
  - - ">="
160
171
  - !ruby/object:Gem::Version
172
+ hash: 3
161
173
  segments:
162
174
  - 0
163
175
  version: "0"
164
- type: :runtime
165
- prerelease: false
176
+ name: rubikon
166
177
  version_requirements: *id011
178
+ prerelease: false
167
179
  - !ruby/object:Gem::Dependency
168
- name: fssm
180
+ type: :runtime
169
181
  requirement: &id012 !ruby/object:Gem::Requirement
170
182
  none: false
171
183
  requirements:
172
184
  - - ">="
173
185
  - !ruby/object:Gem::Version
186
+ hash: 3
174
187
  segments:
175
188
  - 0
176
189
  version: "0"
177
- type: :runtime
178
- prerelease: false
190
+ name: fssm
179
191
  version_requirements: *id012
192
+ prerelease: false
180
193
  - !ruby/object:Gem::Dependency
181
- name: jsmin
194
+ type: :runtime
182
195
  requirement: &id013 !ruby/object:Gem::Requirement
183
196
  none: false
184
197
  requirements:
185
198
  - - ">="
186
199
  - !ruby/object:Gem::Version
200
+ hash: 3
187
201
  segments:
188
202
  - 0
189
203
  version: "0"
190
- type: :runtime
191
- prerelease: false
204
+ name: jsmin
192
205
  version_requirements: *id013
206
+ prerelease: false
193
207
  - !ruby/object:Gem::Dependency
194
- name: sprockets
208
+ type: :runtime
195
209
  requirement: &id014 !ruby/object:Gem::Requirement
196
210
  none: false
197
211
  requirements:
198
212
  - - ">="
199
213
  - !ruby/object:Gem::Version
214
+ hash: 3
200
215
  segments:
201
216
  - 0
202
217
  version: "0"
203
- type: :runtime
204
- prerelease: false
218
+ name: sprockets
205
219
  version_requirements: *id014
220
+ prerelease: false
206
221
  - !ruby/object:Gem::Dependency
207
- name: rspec
222
+ type: :development
208
223
  requirement: &id015 !ruby/object:Gem::Requirement
209
224
  none: false
210
225
  requirements:
211
226
  - - ">="
212
227
  - !ruby/object:Gem::Version
228
+ hash: 3
213
229
  segments:
214
230
  - 0
215
231
  version: "0"
216
- type: :development
217
- prerelease: false
232
+ name: rspec
218
233
  version_requirements: *id015
234
+ prerelease: false
219
235
  description: Ninjs is a ruby application and small javascript framework that helps you build clean, modular javascript applications. Ninjs encourages "Good Parts" best practices and the Crockford school Module pattern (http://www.crockford.com/). The ninjs command line application is an automatic compiler, written in ruby, and based on the Sprockets library (http://getsprockets.org/).
220
236
  email: daytonn@gmail.com
221
237
  executables:
@@ -526,7 +542,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
526
542
  requirements:
527
543
  - - ">="
528
544
  - !ruby/object:Gem::Version
529
- hash: 3857615926705815432
545
+ hash: 3
530
546
  segments:
531
547
  - 0
532
548
  version: "0"
@@ -535,13 +551,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
535
551
  requirements:
536
552
  - - ">="
537
553
  - !ruby/object:Gem::Version
554
+ hash: 3
538
555
  segments:
539
556
  - 0
540
557
  version: "0"
541
558
  requirements: []
542
559
 
543
560
  rubyforge_project: nowarning
544
- rubygems_version: 1.3.7
561
+ rubygems_version: 1.5.2
545
562
  signing_key:
546
563
  specification_version: 3
547
564
  summary: ninjs is a command line application to help you write clean, modular javascript applications.