actioncable-bindings 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.blade.yml +19 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +102 -0
- data/ROADMAP.md +6 -0
- data/Rakefile +10 -0
- data/actioncable-bindings.gemspec +29 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/actioncable/bindings.rb +8 -0
- data/lib/actioncable/bindings/engine.rb +8 -0
- data/lib/actioncable/bindings/version.rb +5 -0
- data/vendor/assets/javascripts/cable_bindings.coffee +31 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9496613ddeddf5f1a48dc01a8c7d4277d021d393
|
4
|
+
data.tar.gz: f0ad964ca472cf28899895b537df77b294372c8c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f5dd5e7cea6c3f7208ff9525155b10caa7613a2e7114e0f36064017df963dee64a21e202a3e8ca15284afe4c9992eab4a320c3970e5ade59299fadb049494830
|
7
|
+
data.tar.gz: c56a0dbe1244ee0da90f1aa59dd410589b8f58e259192d01db3795fd3ab4643019d993e1b95049ab80398533f0505db08454ce8c12604e9234bf9ba091aedad4
|
data/.blade.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
load_paths:
|
2
|
+
- vendor/assets/javascripts
|
3
|
+
- test
|
4
|
+
|
5
|
+
logical_paths:
|
6
|
+
- setup.js
|
7
|
+
- cable_bindings.js
|
8
|
+
- test.js
|
9
|
+
|
10
|
+
gem_paths:
|
11
|
+
jquery-rails:
|
12
|
+
- vendor/assets/javascripts
|
13
|
+
actioncable:
|
14
|
+
- lib/assets/javascripts
|
15
|
+
build:
|
16
|
+
logical_paths:
|
17
|
+
- cable_bindings.js
|
18
|
+
path: dist
|
19
|
+
js_compressor: uglifier
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Lachlan Sylvester
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Actioncable::Bindings
|
2
|
+
|
3
|
+
Provides DOM bindings for [Action Cable](https://github.com/rails/actioncable)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
This depends on Action Cable wich has not yet been released. You will need to add the Action Cable as a git dependency in addition to adding this gem to the Gemfile.
|
8
|
+
|
9
|
+
Add these lines to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'actioncable', github: 'rails/actioncable'
|
13
|
+
gem 'actioncable-bindings'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Add cable_bindings to your `app/assets/javascripts/application.js` file after the jQuery require
|
23
|
+
|
24
|
+
```coffee
|
25
|
+
//= require jquery
|
26
|
+
//= require cable_bindings
|
27
|
+
```
|
28
|
+
|
29
|
+
Add a cable metatag to point to your Action Cable server
|
30
|
+
|
31
|
+
```html
|
32
|
+
<meta name="cable-uri" value="ws://localhost:28080">
|
33
|
+
```
|
34
|
+
|
35
|
+
Then, start creating subscriptions by adding `data-cable-subscribe` attributes your tags, with the value being the name of the channel you want to subscribe to.
|
36
|
+
|
37
|
+
```html
|
38
|
+
<div data-cable-subscribe="ChatChannel">
|
39
|
+
...
|
40
|
+
</div>
|
41
|
+
```
|
42
|
+
|
43
|
+
You can also create a subscribion using extra parameters by JSON encoding a object.
|
44
|
+
|
45
|
+
```html
|
46
|
+
<div data-cable-subscribe="{"channel"e;: "e;ChatChannel"e;, "room"e;: "e;Best Room"e;}">
|
47
|
+
...
|
48
|
+
</div>
|
49
|
+
```
|
50
|
+
|
51
|
+
Once you have created a subscription, the element will start triggering `cable:received` events whenever data is broadcast through the channel. You can subscribe to these events with something like
|
52
|
+
|
53
|
+
```coffee
|
54
|
+
$(document).on "cable:received", "#chat", (data)->
|
55
|
+
# Do stuff with data
|
56
|
+
```
|
57
|
+
|
58
|
+
### Updating the page.
|
59
|
+
|
60
|
+
Whenever the page changes and a `data-cable-subscripe` element has been added to or removed from the page, the bindings need to be updated. You can do this by calling
|
61
|
+
|
62
|
+
```coffee
|
63
|
+
Cable.bindings.refresh
|
64
|
+
```
|
65
|
+
|
66
|
+
If you are using Turbolinks, this is automattically done for you whenever the `page:chage` event is triggered.
|
67
|
+
|
68
|
+
### Performing Actions
|
69
|
+
|
70
|
+
You can perform actions on a subscription by triggering the `cable:perform` event on the element.
|
71
|
+
|
72
|
+
```coffee
|
73
|
+
$('#my-subscribed-element').trigger("cable:perform", "myAction")
|
74
|
+
```
|
75
|
+
|
76
|
+
Any element with a `data-cable-perform` attribute will automatically perform the action in any parent element with a subscription when clicked.
|
77
|
+
|
78
|
+
For example,
|
79
|
+
|
80
|
+
```html
|
81
|
+
<div data-cable-subscribe="ChatChannel">
|
82
|
+
<a data-cable-perform="away">Appear Away</a>
|
83
|
+
</div>
|
84
|
+
```
|
85
|
+
|
86
|
+
will perform the `away` action on the `ChatChannel` subscription when the link is clicked.
|
87
|
+
|
88
|
+
## Development
|
89
|
+
|
90
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec blade` and visit http://localhost:9876/ to run the javascript tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
91
|
+
|
92
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/lsylvester/actioncable-bindings. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
97
|
+
|
98
|
+
|
99
|
+
## License
|
100
|
+
|
101
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
102
|
+
|
data/ROADMAP.md
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
# Roadmap
|
2
|
+
|
3
|
+
* [ ] Make forms perform actions on submit instead of click
|
4
|
+
* [ ] Allow events that perform actions to be customized with `data-cable-on` attributes.
|
5
|
+
* [ ] Restrict `data-cable-action` click to elements that click would normally trigger events (like `a` tags)
|
6
|
+
* [ ] Support `data-cable-action` in input, textarea, select ect, with default action on change
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'actioncable/bindings/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "actioncable-bindings"
|
8
|
+
spec.version = Actioncable::Bindings::VERSION
|
9
|
+
spec.authors = ["Lachlan Sylvester"]
|
10
|
+
spec.email = ["lachlan.sylvester@hypothetical.com.au"]
|
11
|
+
|
12
|
+
spec.summary = %q{DOM bindings for Action Cable}
|
13
|
+
spec.description = %q{Manage Action Cable subscriptions using data-* attributes}
|
14
|
+
spec.homepage = "https://github.com/lsylvester/actioncable-bindings"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "railties", '>= 4.2.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.8"
|
27
|
+
spec.add_development_dependency "jquery-rails", "~> 4.0"
|
28
|
+
|
29
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "actioncable/bindings"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#= require cable
|
2
|
+
|
3
|
+
class Bindings
|
4
|
+
setupConsumer: =>
|
5
|
+
@consumer = Cable.createConsumer $('meta[name=cable-uri]').attr('value')
|
6
|
+
|
7
|
+
refresh: =>
|
8
|
+
for subscription in @consumer.subscriptions.subscriptions
|
9
|
+
if subscription.element and !$.contains(document, subscription.element)
|
10
|
+
subscription.unsubscribe()
|
11
|
+
|
12
|
+
for element in $('[data-cable-subscribe]')
|
13
|
+
element.subscription ||= @consumer.subscriptions.create $(element).data('cable-subscribe'),
|
14
|
+
element: element,
|
15
|
+
|
16
|
+
received: (data)->
|
17
|
+
$(element).trigger("cable:received", data)
|
18
|
+
|
19
|
+
connected: ->
|
20
|
+
$(element).trigger("cable:connected")
|
21
|
+
|
22
|
+
$(element).on 'cable:perform', (e, action, params)->
|
23
|
+
element.subscription.perform action, params
|
24
|
+
|
25
|
+
Cable.bindings = new Bindings
|
26
|
+
|
27
|
+
$(document).on 'click', "[data-cable-action]", (e)->
|
28
|
+
$(this).trigger 'cable:perform', $(this).data('cable-action')
|
29
|
+
|
30
|
+
$(document).ready Cable.bindings.setupConsumer
|
31
|
+
$(document).on "page:change", Cable.bindings.refresh
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: actioncable-bindings
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lachlan Sylvester
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: jquery-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.0'
|
83
|
+
description: Manage Action Cable subscriptions using data-* attributes
|
84
|
+
email:
|
85
|
+
- lachlan.sylvester@hypothetical.com.au
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".blade.yml"
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- ROADMAP.md
|
98
|
+
- Rakefile
|
99
|
+
- actioncable-bindings.gemspec
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- lib/actioncable/bindings.rb
|
103
|
+
- lib/actioncable/bindings/engine.rb
|
104
|
+
- lib/actioncable/bindings/version.rb
|
105
|
+
- vendor/assets/javascripts/cable_bindings.coffee
|
106
|
+
homepage: https://github.com/lsylvester/actioncable-bindings
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.4.5
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: DOM bindings for Action Cable
|
130
|
+
test_files: []
|