rails_script 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64a22187ede1845168cb8ad0acddc07dee947b73
4
- data.tar.gz: 251b6ec058a974335534d538c12448ba35d91384
3
+ metadata.gz: 2d9958511d98ea471627dc977879e6d035c35898
4
+ data.tar.gz: 25902692ba8bb9c73879b978090a30cc26440668
5
5
  SHA512:
6
- metadata.gz: da8b34691b63e3162e69197afbb81bdcd7037abeae3dbef05c2cb9d844aab665b0faf489bf174463d4bfdddc3299559938746524803f8d40e6628e3656835200
7
- data.tar.gz: 7725be6085598c6768ca4889d9417d33c3dc0b748265ddd03a3fa4dc927d5df8b2ff7a6a539d89127bf7c853b8d138f349438a87ac2d89ada13a602129efa283
6
+ metadata.gz: 3bae329972b4ade7e5f3ec70ec9a681b9b06f3c1d3a86c9d39bde356af002715e4a919f973a36c0f089aed29ed19ba1646cf3ec201b5078cec956f3186d483d9
7
+ data.tar.gz: 26a1d353e5466280a883128165935bd41f7baa25b322625d548a71531d171f70309ceb0faf77533359b357feec44846adfc02b2db52f9716f6237f23e8706159
data/README.md CHANGED
@@ -280,6 +280,10 @@ class App.MyClassName
280
280
 
281
281
  ```
282
282
 
283
+ ### Passing Rails Variables
284
+
285
+ To pass data from Rails to JavaScript, just define ```@to_javascript```. This is then converted to a JSON object with ```@to_javascript.to_json``` and can be accessed with ```Utility.RailsVars```.
286
+
283
287
 
284
288
  ### Events
285
289
 
@@ -1,40 +1,9 @@
1
1
  window.Utility || (window.Utility = {});
2
2
  Utility.RailsVars = #{@to_javascript.nil? ? '{}' : @to_javascript.to_json};
3
3
 
4
- jQuery(function() {
4
+ function() {
5
5
  window.$this = new (App.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || App.Base)();
6
6
  if (typeof $this.#{ action_name } === 'function') {
7
7
  return $this.#{ action_name }.call();
8
8
  }
9
- });
10
-
11
- jQuery(document).on('page:before-change', function() {
12
- var element, handler, handlers, type, _i, _len, _ref, _results;
13
- _ref = [window, document];
14
- _results = [];
15
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
16
- element = _ref[_i];
17
- _results.push((function() {
18
- var _ref2, _results2;
19
- _ref2 = jQuery._data(element, 'events');
20
- _results2 = [];
21
- for (type in _ref2) {
22
- handlers = _ref2[type];
23
- _results2.push((function() {
24
- var _j, _len2, _results3;
25
- _results3 = [];
26
- for (_j = 0, _len2 = handlers.length; _j < _len2; _j++) {
27
- handler = handlers[_j];
28
- if (handler == null) {
29
- continue;
30
- }
31
- _results3.push(handler.namespace === '' ? $(element).off(type, handler.handler) : void 0);
32
- }
33
- return _results3;
34
- })());
35
- }
36
- return _results2;
37
- })());
38
- }
39
- return _results;
40
- });
9
+ };
@@ -2,5 +2,4 @@ window.App ||= {}
2
2
  class App.<%= class_name.gsub('::', '') %>
3
3
 
4
4
  constructor: ->
5
- super
6
5
  return this
@@ -2,5 +2,5 @@ window.Element ||= {}
2
2
  class Element.<%= element_name.gsub('::', '') %> <%= "extends Utility.#{utility.gsub('::', '')}" unless utility.blank? %>
3
3
 
4
4
  constructor: ->
5
- super
5
+ <%= "super" unless utility.blank? %>
6
6
  return this
@@ -2,14 +2,36 @@ window.App ||= {}
2
2
  class App.Base
3
3
 
4
4
  constructor: ->
5
+ if (window.jQuery) then @setClearEventHandlers() # clearing application event handlers only possible with jQuery
5
6
  return this
6
7
 
7
8
 
9
+ ###
10
+ Run the new action for the create action. Generally the create action will 'render :new' if there was a problem.
11
+ This prevents doubling the code for each action.
12
+ ###
8
13
  create: ->
9
14
  if typeof $this.new == 'function'
10
15
  return $this.new()
11
16
 
12
17
 
18
+ ###
19
+ Run the edit action for the update action. Generally the update action will 'render :edit' if there was a problem.
20
+ This prevents doubling the code for each action.
21
+ ###
13
22
  update: ->
14
23
  if typeof $this.edit == 'function'
15
- return $this.edit()
24
+ return $this.edit()
25
+
26
+
27
+ ###
28
+ Clear event handlers with a blank namespace. This will prevent window and document event handlers from stacking
29
+ when each new page is fetched. Adding a namespace to your events will prevent them from being removed between page
30
+ loads, i.e. "$(window).on 'scroll.app', myHandler"
31
+ ###
32
+ clearEventHandlers: ->
33
+ jQuery(document).on 'page:before-change', ->
34
+ for element in [window, document]
35
+ for event, handlers in jQuery._data(element, 'events')
36
+ for handler in handlers
37
+ if handler.namespace == '' then $(element).off event, handler.handler
@@ -2,5 +2,4 @@ window.Utility ||= {}
2
2
  class Utility.<%= utility_name.gsub('::', '') %>
3
3
 
4
4
  constructor: ->
5
- super
6
5
  return this
@@ -3,8 +3,16 @@ module RailsScript
3
3
 
4
4
  def include_rails_script
5
5
  javascript_tag <<-RUBY
6
- window.Utility || (window.Utility = {});Utility.RailsVars = #{@to_javascript.nil? ? '{}' : @to_javascript.to_json};jQuery(function(){window.$this=new(App.#{controller_path.split(/\/|_/).map(&:capitalize).join('')}||App.Base);if(typeof $this.#{action_name}==="function"){return $this.#{action_name}.call()}});jQuery(document).on("page:before-change",function(){var e,t,n,r,i,s,o,u;o=[window,document];u=[];for(i=0,s=o.length;i<s;i++){e=o[i];u.push(function(){var i,s;i=jQuery._data(e,"events");s=[];for(r in i){n=i[r];s.push(function(){var i,s,o;o=[];for(i=0,s=n.length;i<s;i++){t=n[i];if(t==null){continue}o.push(t.namespace===""?$(e).off(r,t.handler):void 0)}return o}())}return s}())}return u})
7
- RUBY
6
+ window.Utility || (window.Utility = {});
7
+ Utility.RailsVars = #{@to_javascript.nil? ? '{}' : @to_javascript.to_json};
8
+
9
+ (function() {
10
+ window.$this = new (App.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || App.Base)();
11
+ if (typeof $this.#{ action_name } === 'function') {
12
+ return $this.#{ action_name }.call();
13
+ }
14
+ })();
15
+ RUBY
8
16
  end
9
17
 
10
18
  end
@@ -1,3 +1,3 @@
1
1
  module RailsScript
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_script
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Pheasey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-07 00:00:00.000000000 Z
11
+ date: 2014-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler