Skip to content

Instantly share code, notes, and snippets.

@gabetax
Created October 11, 2013 16:51
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 gabetax/6938181 to your computer and use it in GitHub Desktop.
Save gabetax/6938181 to your computer and use it in GitHub Desktop.
Javascript Page Object Example
<% @body_data = {
page_object: 'PostForm',
rails_env: Rails.env,
post_max_length: Post.column_types['title'].limit
}
%>
<!DOCTYPE html>
<html>
<head>
<title>Csp</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<%= tag 'body', { data: @body_data || {} }, true %>
<%= yield %>
</body>
</html>
$ ->
dispatchPageObjects()
dispatchPageObjects = ->
bodyData = $('body').data()
pageObjectClass = bodyData.pageObject
# window[foo] does String to Function conversion
new window[pageObjectClass](bodyData).start() if pageObjectClass
class @PostForm
constructor: ({@railsEnv, @postMaxLength}) ->
start: ->
@showInjectedValues()
@makeLabelsRed()
showInjectedValues: ->
alert "in #{@railsEnv} titles can only be #{@postMaxLength} characters long"
makeLabelsRed: ->
$('label').css 'color', 'red'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment