Created
September 21, 2015 16:42
-
-
Save jaeming/a23a51a5cb5b46cbdab2 to your computer and use it in GitHub Desktop.
opal 101 ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Opal Experiments</title> | |
<link rel="stylesheet" href="main.css"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/opal/0.3.43/opal.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/opal-parser/0.3.43/opal-parser.min.js"></script> | |
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/opal-jquery/0.0.8/opal-jquery.min.js"></script> | |
</head> | |
<body> | |
<div id="posts"></div> | |
<script type="text/ruby"> | |
module Render | |
def self.posts_index(posts_data) | |
location = Post.posts_section | |
posts_data.each do |post| | |
template = Post.posts_template(post) | |
location.append(template) | |
end | |
end | |
end | |
class Post | |
def self.posts_resource | |
"http://benji.herokuapp.com/api/articles/" | |
end | |
def self.posts_section | |
Element.find('#posts') | |
end | |
def self.posts_template(post) | |
"<div class='entry'>"+ | |
"<h2>#{post['title']}</h2>"+ | |
"<p>#{post['text']}</p>"+ | |
"</div>" | |
end | |
end | |
class PostsController < Post | |
include Render | |
def self.index | |
HTTP.get(Post.posts_resource) do |response| | |
data = response.json["articles"] | |
Render.posts_index(data) | |
end | |
end | |
end | |
PostsController.index | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.entry { | |
padding: .4rem .8rem; | |
background: #ededed; | |
border-radius: 6px; | |
margin: .2rem 3rem 1.6rem 3rem; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment