autoexec_bat 0.0.1 → 0.0.2
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.
- data/README.md +40 -14
- data/lib/assets/javascripts/autoexec_bat.coffee +8 -8
- data/lib/autoexec_bat/version.rb +1 -1
- metadata +4 -9
data/README.md
CHANGED
@@ -5,28 +5,28 @@ AutoexecBat was written to aid organizing and running javascript code using the
|
|
5
5
|
The main idea is to organize all javascript code into separate namespaces and let a data-autoexec attribute determine which module to run.
|
6
6
|
|
7
7
|
Some examples:
|
8
|
-
|
8
|
+
|
9
9
|
define "App.Products", (exports) ->
|
10
10
|
exports.autoexec = ->
|
11
11
|
# this is the autoexec function that AutoexecBat looks for
|
12
|
-
|
12
|
+
|
13
13
|
# Module with private methods
|
14
14
|
define "App.Products.Index", (exports) ->
|
15
15
|
exports.autoexec = ->
|
16
16
|
setupEventListeners()
|
17
17
|
setupSomethingElse()
|
18
18
|
takeItAway()
|
19
|
-
|
19
|
+
|
20
20
|
setupEventListeners = ->
|
21
21
|
$('table tbody td').on 'click', -> # event handler
|
22
22
|
$('input').on 'click', -> # another event handler
|
23
|
-
|
23
|
+
|
24
24
|
takeItAway = ->
|
25
25
|
$('tag').doSomething()
|
26
26
|
|
27
27
|
setupSomethingElse = ->
|
28
28
|
$('.private').show()
|
29
|
-
|
29
|
+
|
30
30
|
# A module with dependencies
|
31
31
|
define "App.Products.Show", ["App.Products"], (exports) ->
|
32
32
|
exports.autoexec = ->
|
@@ -35,7 +35,7 @@ Some examples:
|
|
35
35
|
|
36
36
|
# If all you want is to run the dependencies:
|
37
37
|
define "App.Gallery", ["App.UI.Fancybox", "App.UI.FileUpload"]
|
38
|
-
|
38
|
+
|
39
39
|
# A simple coffeescript class:
|
40
40
|
class namespace("App.Models").Product
|
41
41
|
# this class will be known as App.Models.Product
|
@@ -43,22 +43,42 @@ Some examples:
|
|
43
43
|
# Run AutoexecBat on all tags with a data-autoexec attribute (using jQuery)
|
44
44
|
jQuery ->
|
45
45
|
$('[data-autoexec]').autoexec()
|
46
|
-
|
47
|
-
#
|
46
|
+
|
47
|
+
# If you're using the jQuery plugin you can fetch the callee in the autoexec function
|
48
|
+
define "App.Products.Show", (exports) ->
|
49
|
+
exports.autoexec = (productItem) ->
|
50
|
+
setupEventListeners(productItem)
|
51
|
+
|
52
|
+
setupEventListeners = (productItem) ->
|
53
|
+
$(productItem).on 'click', -> # specific event handler
|
54
|
+
|
55
|
+
# y u no got jquery?
|
48
56
|
# Run it manually or use whatever
|
49
57
|
AutoexecBat.run "App.Products.Show"
|
50
|
-
# or
|
58
|
+
# or
|
51
59
|
App.Products.Show.autoexec()
|
52
60
|
|
61
|
+
# To push the callee to autoexec
|
62
|
+
AutoexecBat.run "App.Products.Show", productItem
|
63
|
+
# or
|
64
|
+
App.Products.Show.autoexec(productItem)
|
65
|
+
|
53
66
|
Finally, just add the module name to a data-autoexec attribute:
|
54
67
|
|
55
68
|
<body data-autoexec="App.Products.Index">
|
56
|
-
|
69
|
+
|
57
70
|
If the given module doesn't exist, it tries to execute a parent module.
|
58
|
-
In this case,
|
71
|
+
In this case, App.Products.autoexec() will be executed:
|
59
72
|
|
60
73
|
<div data-autoexec="App.Products.Dev.Null">..</div>
|
61
|
-
|
74
|
+
|
75
|
+
### Auto-require
|
76
|
+
|
77
|
+
If you have a module that you want to be included always, you can instruct AutoexecBat to require it:
|
78
|
+
|
79
|
+
AutoexecBat.autoRequire = "App.My.Module"
|
80
|
+
|
81
|
+
Note: this will only work with modules defined with define().
|
62
82
|
|
63
83
|
## Installation
|
64
84
|
|
@@ -66,13 +86,19 @@ Add this line to your application's Gemfile:
|
|
66
86
|
|
67
87
|
gem 'autoexec_bat'
|
68
88
|
|
69
|
-
Require your modules and activate autoexecution:
|
89
|
+
Require your modules and activate autoexecution (with turbolinks - default in Rails 4):
|
70
90
|
|
71
91
|
#= require autoexec_bat
|
72
92
|
#= require_tree ./folder-containing-your-modules
|
73
|
-
jQuery ->
|
93
|
+
jQuery(document).bind 'ready page:change', ->
|
74
94
|
$('[data-autoexec]').autoexec()
|
75
95
|
|
96
|
+
For Rails 3:
|
97
|
+
|
98
|
+
#= require autoexec_bat
|
99
|
+
#= require_tree ./folder-containing-your-modules
|
100
|
+
jQuery ->
|
101
|
+
$('[data-autoexec]').autoexec()
|
76
102
|
|
77
103
|
## Contributing
|
78
104
|
|
@@ -28,10 +28,10 @@ AutoexecBat =
|
|
28
28
|
target.loaded = false
|
29
29
|
target.autoexec = -> # make sure the autoexec function exists
|
30
30
|
target.dependencies = dependencies
|
31
|
-
target.init = ->
|
32
|
-
unless @loaded
|
31
|
+
target.init = (callee) ->
|
32
|
+
unless @loaded
|
33
33
|
require dependencies
|
34
|
-
@autoexec() if typeof @autoexec is 'function'
|
34
|
+
@autoexec(callee) if typeof @autoexec is 'function'
|
35
35
|
@autoexec = -> AutoexecBat.log "Module already initialized"
|
36
36
|
@loaded = true
|
37
37
|
|
@@ -55,13 +55,13 @@ AutoexecBat =
|
|
55
55
|
module = module[item] unless typeof module[item] is 'undefined'
|
56
56
|
module
|
57
57
|
|
58
|
-
initializeModule: (nameOrModule) ->
|
58
|
+
initializeModule: (nameOrModule, callee) ->
|
59
59
|
module = if typeof nameOrModule is "string" then AutoexecBat.findModule(nameOrModule) else nameOrModule
|
60
|
-
module.init() if typeof module isnt 'undefined' and typeof module.init isnt 'undefined'
|
60
|
+
module.init(callee) if typeof module isnt 'undefined' and typeof module.init isnt 'undefined'
|
61
61
|
|
62
|
-
run: (name) ->
|
62
|
+
run: (name, callee) ->
|
63
63
|
module = AutoexecBat.findModule name
|
64
|
-
AutoexecBat.initializeModule module
|
64
|
+
AutoexecBat.initializeModule module, callee
|
65
65
|
|
66
66
|
|
67
67
|
# Globals
|
@@ -75,4 +75,4 @@ unless typeof jQuery is 'undefined'
|
|
75
75
|
$ = jQuery
|
76
76
|
$.fn.extend
|
77
77
|
autoexec: (options) ->
|
78
|
-
return @each -> AutoexecBat.run $(@).data('autoexec')
|
78
|
+
return @each -> AutoexecBat.run $(@).data('autoexec'), @
|
data/lib/autoexec_bat/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: autoexec_bat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-09 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Autoexecution of javascript based on data attribute
|
15
15
|
email:
|
@@ -41,22 +41,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
41
|
- - ! '>='
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: '0'
|
44
|
-
segments:
|
45
|
-
- 0
|
46
|
-
hash: 1279658281685689202
|
47
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
45
|
none: false
|
49
46
|
requirements:
|
50
47
|
- - ! '>='
|
51
48
|
- !ruby/object:Gem::Version
|
52
49
|
version: '0'
|
53
|
-
segments:
|
54
|
-
- 0
|
55
|
-
hash: 1279658281685689202
|
56
50
|
requirements: []
|
57
51
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.8.
|
52
|
+
rubygems_version: 1.8.24
|
59
53
|
signing_key:
|
60
54
|
specification_version: 3
|
61
55
|
summary: Autoexecution of javascript based on data attribute
|
62
56
|
test_files: []
|
57
|
+
has_rdoc:
|