Module: RenderSync::ViewHelpers
- Defined in:
- lib/render_sync/view_helpers.rb
Instance Method Summary collapse
-
#sync(options = {}) ⇒ Object
Surround partial render in script tags, watching for sync_update and sync_destroy channels from pubsub server.
-
#sync_new(options = {}) ⇒ Object
Setup listener for new resource from sync_new channel, appending partial in place.
Instance Method Details
#sync(options = {}) ⇒ Object
Surround partial render in script tags, watching for sync_update and sync_destroy channels from pubsub server
options - The Hash of options
partial - The String partial filename without leading underscore
resource - The ActiveModel resource
collection - The Array of ActiveModel resources to use in place of
single resource
Examples
<%= sync partial: 'todo', resource: todo %>
<%= sync partial: 'todo', collection: todos %>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/render_sync/view_helpers.rb', line 18 def sync( = {}) collection = [:collection] || [.fetch(:resource)] scope = [:channel] || [:scope] || (collection.is_a?(RenderSync::Scope) ? collection : nil) partial_name = .fetch(:partial, scope) refetch = .fetch(:refetch, false) results = [] collection.each do |resource| if refetch partial = RefetchPartial.new(partial_name, resource, scope, self) else partial = Partial.new(partial_name, resource, scope, self) end results << " <script type='text/javascript' data-sync-id='#{partial.selector_start}'> RenderSync.onReady(function(){ var partial = new RenderSync.Partial({ name: '#{partial.name}', resourceName: '#{partial.resource.name}', resourceId: '#{resource.id}', authToken: '#{partial.refetch_auth_token}', channelUpdate: '#{partial.channel_for_action(:update)}', channelDestroy: '#{partial.channel_for_action(:destroy)}', selectorStart: '#{partial.selector_start}', selectorEnd: '#{partial.selector_end}', refetch: #{refetch} }); partial.subscribe(); }); </script> ".squish.html_safe results << partial.render results << " <script type='text/javascript' data-sync-id='#{partial.selector_end}'> </script> ".squish.html_safe end safe_join(results) end |
#sync_new(options = {}) ⇒ Object
Setup listener for new resource from sync_new channel, appending partial in place
options - The Hash of options
partial - The String partial filename without leading underscore
resource - The ActiveModel resource
scope - The ActiveModel resource to scope the new channel publishes to.
Used for restricting new resource publishes to 'owner' models.
ie, current_user, project, group, etc. When excluded, listens
for global resource creates.
direction - The String/Symbol direction to insert rendered partials.
One of :append, :prepend. Defaults to :append
Examples
<%= sync_new partial: 'todo', resource: Todo.new, scope: @project %>
<%= sync_new partial: 'todo', resource: Todo.new, scope: @project, direction: :prepend %>
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/render_sync/view_helpers.rb', line 77 def sync_new( = {}) partial_name = .fetch(:partial) scope = [:scope] direction = .fetch :direction, 'append' refetch = .fetch(:refetch, false) resource = scope.is_a?(RenderSync::Scope) ? scope.new : .fetch(:resource) if refetch creator = RefetchPartialCreator.new(partial_name, resource, scope, self) else creator = PartialCreator.new(partial_name, resource, scope, self) end " <script type='text/javascript' data-sync-id='#{creator.selector}'> RenderSync.onReady(function(){ var creator = new RenderSync.PartialCreator({ name: '#{partial_name}', resourceName: '#{creator.resource.name}', channel: '#{creator.channel}', selector: '#{creator.selector}', direction: '#{direction}', refetch: #{refetch} }); creator.subscribe(); }); </script> ".html_safe end |