canjs-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,292 @@
1
+ (function(can, window, undefined){
2
+
3
+ //---- ADD jQUERY HELPERS -----
4
+ //converts jquery functions to use views
5
+ var convert, modify, isTemplate, isHTML, isDOM, getCallback,
6
+ // text and val cannot produce an element, so don't run hookups on them
7
+ noHookup = {'val':true,'text':true};
8
+
9
+ convert = function( func_name ) {
10
+ // save the old jQuery helper
11
+ var old = $.fn[func_name];
12
+
13
+ // replace it with our new helper
14
+ $.fn[func_name] = function() {
15
+
16
+ var args = can.makeArray(arguments),
17
+ callbackNum,
18
+ callback,
19
+ self = this,
20
+ result;
21
+
22
+ // if the first arg is a deferred
23
+ // wait until it finishes, and call
24
+ // modify with the result
25
+ if ( can.isDeferred(args[0]) ) {
26
+ args[0].done(function( res ) {
27
+ modify.call(self, [res], old);
28
+ })
29
+ return this;
30
+ }
31
+ //check if a template
32
+ else if ( isTemplate(args) ) {
33
+
34
+ // if we should operate async
35
+ if ((callbackNum = getCallback(args))) {
36
+ callback = args[callbackNum];
37
+ args[callbackNum] = function( result ) {
38
+ modify.call(self, [result], old);
39
+ callback.call(self, result);
40
+ };
41
+ can.view.apply(can.view, args);
42
+ return this;
43
+ }
44
+ // call view with args (there might be deferreds)
45
+ result = can.view.apply(can.view, args);
46
+
47
+ // if we got a string back
48
+ if (!can.isDeferred(result) ) {
49
+ // we are going to call the old method with that string
50
+ args = [result];
51
+ } else {
52
+ // if there is a deferred, wait until it is done before calling modify
53
+ result.done(function( res ) {
54
+ modify.call(self, [res], old);
55
+ })
56
+ return this;
57
+ }
58
+ }
59
+ return noHookup[func_name] ? old.apply(this,args) :
60
+ modify.call(this, args, old);
61
+ };
62
+ };
63
+
64
+ // modifies the content of the element
65
+ // but also will run any hookup
66
+ modify = function( args, old ) {
67
+ var res, stub, hooks;
68
+
69
+ //check if there are new hookups
70
+ for ( var hasHookups in can.view.hookups ) {
71
+ break;
72
+ }
73
+
74
+ //if there are hookups, turn into a frag
75
+ // and insert that
76
+ // by using a frag, the element can be recursively hooked up
77
+ // before insterion
78
+ if ( hasHookups && args[0] && isHTML(args[0]) ) {
79
+ args[0] = can.view.frag(args[0])
80
+ }
81
+
82
+ //then insert into DOM
83
+ res = old.apply(this, args);
84
+
85
+ return res;
86
+ };
87
+
88
+ // returns true or false if the args indicate a template is being used
89
+ // $('#foo').html('/path/to/template.ejs',{data})
90
+ // in general, we want to make sure the first arg is a string
91
+ // and the second arg is data
92
+ isTemplate = function( args ) {
93
+ // save the second arg type
94
+ var secArgType = typeof args[1];
95
+
96
+ // the first arg is a string
97
+ return typeof args[0] == "string" &&
98
+ // the second arg is an object or function
99
+ (secArgType == 'object' || secArgType == 'function') &&
100
+ // but it is not a dom element
101
+ !isDOM(args[1]);
102
+ };
103
+ // returns true if the arg is a jQuery object or HTMLElement
104
+ isDOM = function(arg){
105
+ return arg.nodeType || (arg[0] && arg[0].nodeType)
106
+ };
107
+ // returns whether the argument is some sort of HTML data
108
+ isHTML = function( arg ) {
109
+ if ( isDOM(arg) ) {
110
+ // if jQuery object or DOM node we're good
111
+ return true;
112
+ } else if ( typeof arg === "string" ) {
113
+ // if string, do a quick sanity check that we're HTML
114
+ arg = can.trim(arg);
115
+ return arg.substr(0, 1) === "<" && arg.substr(arg.length - 1, 1) === ">" && arg.length >= 3;
116
+ } else {
117
+ // don't know what you are
118
+ return false;
119
+ }
120
+ };
121
+
122
+ //returns the callback arg number if there is one (for async view use)
123
+ getCallback = function( args ) {
124
+ return typeof args[3] === 'function' ? 3 : typeof args[2] === 'function' && 2;
125
+ };
126
+
127
+ /**
128
+ * @add jQuery.fn
129
+ * @parent can.View
130
+ * Called on a jQuery collection that was rendered with can.View with pending hookups. can.View can render a
131
+ * template with hookups, but not actually perform the hookup, because it returns a string without actual DOM
132
+ * elements to hook up to. So hookup performs the hookup and clears the pending hookups, preventing errors in
133
+ * future templates.
134
+ *
135
+ * @codestart
136
+ * $(can.View('//views/recipes.ejs',recipeData)).hookup()
137
+ * @codeend
138
+ */
139
+ $.fn.hookup = function() {
140
+ can.view.frag(this);
141
+ return this;
142
+ };
143
+
144
+ /**
145
+ * @add jQuery.fn
146
+ */
147
+ can.each([
148
+ /**
149
+ * @function jQuery.fn.prepend
150
+ * @parent can.view.modifiers
151
+ *
152
+ * Extending the original [http://api.jquery.com/prepend/ jQuery().prepend()]
153
+ * to render [can.view] templates inserted at the beginning of each element in the set of matched elements.
154
+ *
155
+ * $('#test').prepend('path/to/template.ejs', { name : 'canjs' });
156
+ *
157
+ * @param {String|Object|Function} content A template filename or the id of a view script tag
158
+ * or a DOM element, array of elements, HTML string, or can object.
159
+ * @param {Object} [data] The data to render the view with.
160
+ * If rendering a view template this parameter always has to be present
161
+ * (use the empty object initializer {} for no data).
162
+ * @param {Function} [callback] A success callback to load the view asynchronously
163
+ *
164
+ * @return {jQuery|can.Deferred} The jQuery object or a [can.Deferred] if a deferred has
165
+ * been passed in data.
166
+ */
167
+ "prepend",
168
+ /**
169
+ * @function jQuery.fn.append
170
+ * @parent can.view.modifiers
171
+ *
172
+ * Extending the original [http://api.jquery.com/append/ jQuery().append()]
173
+ * to render [can.view] templates inserted at the end of each element in the set of matched elements.
174
+ *
175
+ * $('#test').append('path/to/template.ejs', { name : 'canjs' });
176
+ *
177
+ * @param {String|Object|Function} content A template filename or the id of a view script tag
178
+ * or a DOM element, array of elements, HTML string, or can object.
179
+ * @param {Object} [data] The data to render the view with.
180
+ * If rendering a view template this parameter always has to be present
181
+ * (use the empty object initializer {} for no data).
182
+ * @param {Function} [callback] A success callback to load the view asynchronously
183
+ *
184
+ * @return {jQuery|can.Deferred} The jQuery object or a [can.Deferred] if a deferred has
185
+ * been passed in data.
186
+ */
187
+ "append",
188
+ /**
189
+ * @function jQuery.fn.after
190
+ * @parent can.view.modifiers
191
+ *
192
+ * Extending the original [http://api.jquery.com/after/ jQuery().after()]
193
+ * to render [can.view] templates inserted after each element in the set of matched elements.
194
+ *
195
+ * $('#test').after('path/to/template.ejs', { name : 'canjs' });
196
+ *
197
+ * @param {String|Object|Function} content A template filename or the id of a view script tag
198
+ * or a DOM element, array of elements, HTML string, or can object.
199
+ * @param {Object} [data] The data to render the view with.
200
+ * If rendering a view template this parameter always has to be present
201
+ * (use the empty object initializer {} for no data).
202
+ * @param {Function} [callback] A success callback to load the view asynchronously
203
+ *
204
+ * @return {jQuery|can.Deferred} The jQuery object or a [can.Deferred] if a deferred has
205
+ * been passed in data.
206
+ */
207
+ "after",
208
+ /**
209
+ * @function jQuery.fn.before
210
+ * @parent can.view.modifiers
211
+ *
212
+ * Extending the original [http://api.jquery.com/before/ jQuery().before()]
213
+ * to render [can.view] templates inserted before each element in the set of matched elements.
214
+ *
215
+ * $('#test').before('path/to/template.ejs', { name : 'canjs' });
216
+ *
217
+ * @param {String|Object|Function} content A template filename or the id of a view script tag
218
+ * or a DOM element, array of elements, HTML string, or can object.
219
+ * @param {Object} [data] The data to render the view with.
220
+ * If rendering a view template this parameter always has to be present
221
+ * (use the empty object initializer {} for no data).
222
+ * @param {Function} [callback] A success callback to load the view asynchronously
223
+ *
224
+ * @return {jQuery|can.Deferred} The jQuery object or a [can.Deferred] if a deferred has
225
+ * been passed in data.
226
+ */
227
+ "before",
228
+ /**
229
+ * @function jQuery.fn.text
230
+ * @parent can.view.modifiers
231
+ *
232
+ * Extending the original [http://api.jquery.com/text/ jQuery().text()]
233
+ * to render [can.View] templates as the content of each matched element.
234
+ * Unlike [jQuery.fn.html] jQuery.fn.text also works with XML, escaping the provided
235
+ * string as necessary.
236
+ *
237
+ * $('#test').text('path/to/template.ejs', { name : 'canjs' });
238
+ *
239
+ * @param {String|Object|Function} content A template filename or the id of a view script tag
240
+ * or a DOM element, array of elements, HTML string, or can object.
241
+ * @param {Object} [data] The data to render the view with.
242
+ * If rendering a view template this parameter always has to be present
243
+ * (use the empty object initializer {} for no data).
244
+ * @param {Function} [callback] A success callback to load the view asynchronously
245
+ *
246
+ * @return {jQuery|can.Deferred} The jQuery object or a [can.Deferred] if a deferred has
247
+ * been passed in data.
248
+ */
249
+ "text",
250
+ /**
251
+ * @function jQuery.fn.html
252
+ * @parent can.view.modifiers
253
+ *
254
+ * Extending the original [http://api.jquery.com/html/ jQuery().html()]
255
+ * to render [can.view] templates as the content of each matched element.
256
+ *
257
+ * $('#test').html('path/to/template.ejs', { name : 'canjs' });
258
+ *
259
+ * @param {String|Object|Function} content A template filename or the id of a view script tag
260
+ * or a DOM element, array of elements, HTML string, or can object.
261
+ * @param {Object} [data] The data to render the view with.
262
+ * If rendering a view template this parameter always has to be present
263
+ * (use the empty object initializer {} for no data).
264
+ * @param {Function} [callback] A success callback to load the view asynchronously
265
+ *
266
+ * @return {jQuery|can.Deferred} The jQuery object or a [can.Deferred] if a deferred has
267
+ * been passed in data.
268
+ */
269
+ "html",
270
+ /**
271
+ * @function jQuery.fn.replaceWith
272
+ * @parent can.view.modifiers
273
+ *
274
+ * Extending the original [http://api.jquery.com/replaceWith/ jQuery().replaceWith()]
275
+ * to render [can.view] templates replacing each element in the set of matched elements.
276
+ *
277
+ * $('#test').replaceWith('path/to/template.ejs', { name : 'canjs' });
278
+ *
279
+ * @param {String|Object|Function} content A template filename or the id of a view script tag
280
+ * or a DOM element, array of elements, HTML string, or can object.
281
+ * @param {Object} [data] The data to render the view with.
282
+ * If rendering a view template this parameter always has to be present
283
+ * (use the empty object initializer {} for no data).
284
+ * @param {Function} [callback] A success callback to load the view asynchronously
285
+ *
286
+ * @return {jQuery|can.Deferred} The jQuery object or a [can.Deferred] if a deferred has
287
+ * been passed in data.
288
+ */
289
+ "replaceWith", "val"],function(func){
290
+ convert(func);
291
+ });
292
+ })(this.can, this )
@@ -0,0 +1,15 @@
1
+ #!/bin/bash
2
+
3
+ wget https://github.com/downloads/jupiterjs/canjs/can.jquery-1.0.7.js
4
+ wget https://github.com/downloads/jupiterjs/canjs/can.jquery-1.0.7.min.js
5
+ wget http://canjs.us/release/latest/can.construct.proxy.js
6
+ wget http://canjs.us/release/latest/can.construct.super.js
7
+ wget http://canjs.us/release/latest/can.observe.delegate.js
8
+ wget http://canjs.us/release/latest/can.observe.setter.js
9
+ wget http://canjs.us/release/latest/can.observe.attributes.js
10
+ wget http://canjs.us/release/latest/can.observe.validations.js
11
+ wget http://canjs.us/release/latest/can.observe.backup.js
12
+ wget http://canjs.us/release/latest/can.control.plugin.js
13
+ wget http://canjs.us/release/latest/can.control.view.js
14
+ wget http://canjs.us/release/latest/can.view.modifiers.js
15
+ wget http://canjs.us/release/latest/can.fixture.js
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canjs-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Craig Wickesser
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: '5.0'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.2.0
33
+ - - <
34
+ - !ruby/object:Gem::Version
35
+ version: '5.0'
36
+ - !ruby/object:Gem::Dependency
37
+ name: thor
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '0.14'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ~>
50
+ - !ruby/object:Gem::Version
51
+ version: '0.14'
52
+ description: This gem provides CanJS for your Rails 3.1+ application.
53
+ email:
54
+ - craig@mindscratch.org
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - .gitignore
60
+ - CHANGELOG.md
61
+ - Gemfile
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - canjs-rails.gemspec
66
+ - lib/canjs-rails.rb
67
+ - lib/canjs/rails.rb
68
+ - lib/canjs/rails/engine.rb
69
+ - lib/canjs/rails/version.rb
70
+ - vendor/assets/javascripts/can.construct.proxy.js
71
+ - vendor/assets/javascripts/can.construct.super.js
72
+ - vendor/assets/javascripts/can.control.plugin.js
73
+ - vendor/assets/javascripts/can.control.view.js
74
+ - vendor/assets/javascripts/can.fixture.js
75
+ - vendor/assets/javascripts/can.jquery.js
76
+ - vendor/assets/javascripts/can.jquery.min.js
77
+ - vendor/assets/javascripts/can.observe.attributes.js
78
+ - vendor/assets/javascripts/can.observe.backup.js
79
+ - vendor/assets/javascripts/can.observe.delegate.js
80
+ - vendor/assets/javascripts/can.observe.setter.js
81
+ - vendor/assets/javascripts/can.observe.validations.js
82
+ - vendor/assets/javascripts/can.view.modifiers.js
83
+ - vendor/assets/javascripts/download_canjs.sh
84
+ homepage: https://github.com/mindscratch/canjs-rails
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.3.6
102
+ requirements: []
103
+ rubyforge_project: canjs-rails
104
+ rubygems_version: 1.8.24
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Use CanJS with Rails 3.1+
108
+ test_files: []