Skip to content

Instantly share code, notes, and snippets.

@adambeynon
Created October 15, 2013 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adambeynon/6994021 to your computer and use it in GitHub Desktop.
Save adambeynon/6994021 to your computer and use it in GitHub Desktop.
vienna + rails = <3
# We want vienna to render our Product#show view...
class ProductController < ApplicationController
def show
@product = Product.find(...)
render :vienna => @product
end
end
# The `vienna` renderer will look for a view (Vienna::View) subclass that
# is named ShowProductView. The `@product` ivar will be set in that view.
# We then render the views/product/show.erb file in the context of our
# ShowProductView
class ShowProductView < Vienna::View
def nicer_title
@product.title + " is really awesome!"
end
end
# show.erb:
#
# <h1><%= nicer_title %></h1>
# Now, the awesome bit! When our html page loads, we will have an instance of
# ShowProductView running on the client, with the @product ivar already setup!
# So, we can then just use the #after_render hook to do some stuff.
class ShowProductView < Vienna::View
def nicer_title
@product.title + " is really awesome!"
end
def after_render
@element.on(:click, '.title') do |evt|
alert "did you like the title? #{nicer_title}"
end
end
end
# ALSO, our events will be setup as well :)
class ShowProductView < Vienna::View
def nicer_title
@product.title + " is really awesome!"
end
on :click, '.title' do |evt|
alert "did you like the title? #{nicer_title}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment