aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/lib/rails/generators/channel/templates
diff options
context:
space:
mode:
authorPrathamesh Sonpatki <csonpatki@gmail.com>2016-05-08 09:22:43 -0500
committerPrathamesh Sonpatki <csonpatki@gmail.com>2016-05-17 18:12:19 +0530
commit63ac6255ba3553b529f4b23846ec756b7a7a746d (patch)
tree926d54c770f2388352095a94c5267841eb52e26c /actioncable/lib/rails/generators/channel/templates
parent1e791705dcea650a034b7ccb4d922f455648202d (diff)
downloadrails-63ac6255ba3553b529f4b23846ec756b7a7a746d.tar.gz
rails-63ac6255ba3553b529f4b23846ec756b7a7a746d.tar.bz2
rails-63ac6255ba3553b529f4b23846ec756b7a7a746d.zip
Cable: Generate .js or .coffee files while generating channel as per the javascript engine of the application
- Now we will detect what javascript engine user is using and based on that we will generate either `.js` or `.coffee` version of the channel file. - This also needs a change in coffee-rails to override the `js_template` method. Related PR https://github.com/rails/coffee-rails/pull/72. - Currently coffee-rails gem sets `config.app_generators.javascript_engine` to `:coffee` and using this information we override the `js_template` to set the extension as `.coffee` in coffee-rails gem. - Using this approach, we can keep the `channel.js` and `channel.coffee` files in the Rails repository itself. - Additionally the `js_template` method can act as public interface for coffee-rails gem to hook into and change the extension to `.coffee` without maintaining the actual asset files. [Prathamesh Sonpatki, Matthew Draper]
Diffstat (limited to 'actioncable/lib/rails/generators/channel/templates')
-rw-r--r--actioncable/lib/rails/generators/channel/templates/assets/channel.js18
1 files changed, 18 insertions, 0 deletions
diff --git a/actioncable/lib/rails/generators/channel/templates/assets/channel.js b/actioncable/lib/rails/generators/channel/templates/assets/channel.js
new file mode 100644
index 0000000000..ab0e68b11a
--- /dev/null
+++ b/actioncable/lib/rails/generators/channel/templates/assets/channel.js
@@ -0,0 +1,18 @@
+App.<%= class_name.underscore %> = App.cable.subscriptions.create("<%= class_name %>Channel", {
+ connected: function() {
+ // Called when the subscription is ready for use on the server
+ },
+
+ disconnected: function() {
+ // Called when the subscription has been terminated by the server
+ },
+
+ received: function(data) {
+ // Called when there's incoming data on the websocket for this channel
+ }<%= actions.any? ? ",\n" : '' %>
+<% actions.each do |action| -%>
+ <%=action %>: function() {
+ return this.perform('<%= action %>');
+ }<%= action == actions[-1] ? '' : ",\n" %>
+<% end -%>
+});