initjs 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,58 @@
1
1
  # Initjs
2
2
 
3
- TODO: Write a gem description
3
+ Init.js is a Ruby Gem that helps run your javascript only in a page that its necessary.
4
+
5
+ Along with Rails, you can make a good structure for your javascript.
6
+ A good example is using the Backbone.js, separating each page into a view of the Backbone.js.
7
+
8
+ Works fine with Turbolinks from Rails.
9
+
10
+
11
+ ## Javascript structure example
12
+
13
+ The structure you need follow is the same of your controller and action.
14
+ You can use a namespace too.
15
+
16
+ ### With Backbone.js
17
+
18
+ ```javascript
19
+ App.Posts.New = Backbone.View.extend({
20
+ initialize: function() {
21
+ // Javascript for the page "posts/new"
22
+ }
23
+ });
24
+
25
+ App.Posts.Show = Backbone.View.extend({
26
+ initialize: function() {
27
+ // Javascript for the page "posts/1"
28
+ }
29
+ });
30
+
31
+ // with namespace
32
+ App.Blog.Posts.Show = Backbone.View.extend({
33
+ initialize: function() {
34
+ // Javascript for the page "blog/posts/1"
35
+ }
36
+ });
37
+ ```
38
+
39
+ ### Without Backbone.js
40
+
41
+ ```javascript
42
+ App.Posts.New = function() {
43
+ // Javascript for the page "posts/new"
44
+ };
45
+
46
+ App.Posts.Show = function() {
47
+ // Javascript for the page "posts/1"
48
+ };
49
+
50
+ // with namespace
51
+ App.Blog.Posts.Show = function() {
52
+ // Javascript for the page "blog/posts/1"
53
+ };
54
+ ```
55
+
4
56
 
5
57
  ## Installation
6
58
 
@@ -12,13 +64,42 @@ And then execute:
12
64
 
13
65
  $ bundle
14
66
 
15
- Or install it yourself as:
67
+ Run the generator:
68
+
69
+ rails generate initjs
70
+
71
+ Add `//= require init.js` to your Javascript manifest file (usually found at `app/assets/javascripts/application.js`).
72
+
16
73
 
17
- $ gem install initjs
18
74
 
19
75
  ## Usage
20
76
 
21
- TODO: Write usage instructions here
77
+ Include the Initjs tag in your application layout (usually found at `app/view/layouts/application.html.erb`) after the body tag.
78
+
79
+ ```erb
80
+ <%= initjs_tag %>
81
+ ```
82
+
83
+ ### The app.js
84
+
85
+ If you have a commom javascript that you need execute every page, you can put in `app/assets/javascripts/app.js.coffee`
86
+
87
+ #### Structure example
88
+
89
+ ```coffee
90
+ App = window.App =
91
+ Common:
92
+ init: ->
93
+ # Something here
94
+ finish: ->
95
+ # Something here
96
+ ```
97
+
98
+
99
+ ## Work left to do
100
+
101
+ * Add proper unit tests
102
+
22
103
 
23
104
  ## Contributing
24
105
 
data/initjs.gemspec CHANGED
@@ -8,12 +8,14 @@ Gem::Specification.new do |gem|
8
8
  gem.version = Initjs::VERSION
9
9
  gem.authors = ["Josemar Davi Luedke"]
10
10
  gem.email = ["josemarluedke@gmail.com"]
11
- gem.description = %q{Init.js for Rails an Backbone.js}
12
- gem.summary = %q{Init.js}
11
+ gem.description = %q{Init.js is a Ruby Gem that helps run your javascript only in a page that its necessary}
12
+ gem.summary = %q{Init.js for your Rails application}
13
13
  gem.homepage = ""
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency('rails', '~> 3.1')
19
21
  end
@@ -0,0 +1,37 @@
1
+ window.Initjs =
2
+ initialize: ->
3
+ infos = $("#init-js")
4
+ controllerClass = infos.data("controller-class")
5
+ controllerName = infos.data("controller-name")
6
+ action = infos.data("action")
7
+ this.execFilter('init')
8
+ this.exec(controllerClass, controllerName, action)
9
+ this.execFilter('finish')
10
+
11
+ exec: (controllerClass, controllerName, action) ->
12
+ namespace = App
13
+
14
+ if controllerClass
15
+ railsNamespace = controllerClass.split("::").slice(0, -1)
16
+ else
17
+ railsNamespace = []
18
+
19
+ for name in railsNamespace
20
+ namespace = namespace[name] if namespace
21
+
22
+ if namespace and controllerName
23
+ controller = namespace[controllerName]
24
+ if controller and View = controller[action]
25
+ App.currentView = window.view = new View()
26
+
27
+ execFilter: (filterName) ->
28
+ if App.Common and typeof App.Common[filterName] == 'function'
29
+ App.Common[filterName]()
30
+
31
+ jQuery ->
32
+ window.Initjs.initialize()
33
+
34
+ unless window.Turbolinks is undefined
35
+ document.addEventListener "page:change", ->
36
+ window.Initjs.initialize()
37
+
@@ -0,0 +1,11 @@
1
+ module Initjs
2
+ module Generators
3
+ class InitjsGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def copy_files
7
+ copy_file "app.js.coffee", "app/assets/javascripts/app.js.coffee"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ App = window.App =
2
+ Common:
3
+ init: ->
4
+ # Something here
5
+ finish: ->
6
+ # Something here
7
+
@@ -0,0 +1,12 @@
1
+ module Initjs
2
+ module Helper
3
+ def initjs_tag
4
+ content_tag 'div', '', { :id => "init-js",
5
+ :"data-controller-class" => controller.class.name,
6
+ :"data-controller-name" => controller.controller_name.camelize,
7
+ :"data-action" => controller.action_name.camelize }
8
+ end
9
+ end
10
+ end
11
+
12
+ ActionView::Base.send :include, Initjs::Helper
@@ -1,3 +1,3 @@
1
1
  module Initjs
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/initjs.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "initjs/version"
2
+ require "initjs/helper"
2
3
 
3
4
  module Initjs
4
- # Your code goes here...
5
+ class Engine < ::Rails::Engine
6
+ end
5
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: initjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,26 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-27 00:00:00.000000000 Z
13
- dependencies: []
14
- description: Init.js for Rails an Backbone.js
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ description: Init.js is a Ruby Gem that helps run your javascript only in a page that
31
+ its necessary
15
32
  email:
16
33
  - josemarluedke@gmail.com
17
34
  executables: []
@@ -24,7 +41,11 @@ files:
24
41
  - README.md
25
42
  - Rakefile
26
43
  - initjs.gemspec
44
+ - lib/assets/javascript/init.js.coffee
45
+ - lib/generators/initjs/initjs_generator.rb
46
+ - lib/generators/initjs/templates/app.js.coffee
27
47
  - lib/initjs.rb
48
+ - lib/initjs/helper.rb
28
49
  - lib/initjs/version.rb
29
50
  homepage: ''
30
51
  licenses: []
@@ -49,5 +70,5 @@ rubyforge_project:
49
70
  rubygems_version: 1.8.24
50
71
  signing_key:
51
72
  specification_version: 3
52
- summary: Init.js
73
+ summary: Init.js for your Rails application
53
74
  test_files: []