Module: Danthes::ViewHelpers
- Defined in:
- lib/danthes/view_helpers.rb
Instance Method Summary collapse
-
#publish_to(channel, data = nil, &block) ⇒ Object
Publish the given data or block to the client by sending a Net::HTTP POST request to the Faye server.
-
#subscribe_to(channel, opts = {}) ⇒ Object
Subscribe the client to the given channel.
Instance Method Details
#publish_to(channel, data = nil, &block) ⇒ Object
Publish the given data or block to the client by sending a Net::HTTP POST request to the Faye server. If a block or string is passed in, it is evaluated as JavaScript on the client. Otherwise it will be converted to JSON for use in a JavaScript callback.
8 9 10 |
# File 'lib/danthes/view_helpers.rb', line 8 def publish_to(channel, data = nil, &block) Danthes.publish_to(channel, data || capture(&block)) end |
#subscribe_to(channel, opts = {}) ⇒ Object
Subscribe the client to the given channel. This generates some JavaScript calling Danthes.sign with the subscription options.
15 16 17 18 19 20 |
# File 'lib/danthes/view_helpers.rb', line 15 def subscribe_to(channel, opts = {}) js_tag = opts.delete(:include_js_tag){ true } subscription = Danthes.subscription(channel: channel) content = raw("if (typeof Danthes != 'undefined') { Danthes.sign(#{subscription.to_json}) }") js_tag ? content_tag('script', content, type: 'text/javascript') : content end |