Skip to content

Commit 64792d8

Browse files
committedDec 6, 2014
Cleanup docs a little
1 parent 7217ebf commit 64792d8

File tree

4 files changed

+264
-23
lines changed

4 files changed

+264
-23
lines changed
 

‎.yardopts

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
opal/**/*.rb
2+
--markup markdown
3+
-
4+
CHANGELOG.md

‎CHANGELOG.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## edge
1+
# EDGE
22

33
* Cleanup HTTP implementation.
44

@@ -15,7 +15,7 @@
1515
* Add Promise support to `HTTP` get/post/put/delete methods. Also remove
1616
`HTTP#callback` and `#errback` methods.
1717

18-
## 0.2.0 2014-03-12
18+
# 0.2.0 - December 3, 2014
1919

2020
* Add `Document.body` and `Document.head` shortcut to element instances.
2121

@@ -29,26 +29,26 @@
2929

3030
* Expose `#detach` method on `Element`.
3131

32-
## 0.1.2 2013-12-01
32+
# 0.1.2 - December 1, 2013
3333

3434
* Support setting html content through `Element#html()`.
3535

3636
* Add `Element` methods: `#get`, `#attr` and `#prop` by aliasing them to
3737
jquery implementations.
3838

39-
## 0.1.1 2013-11-11
39+
# 0.1.1 - November 11, 2013
4040

4141
* Require `native` from stdlib for `HTTP` to use.
4242

43-
## 0.1.0 2013-11-03
43+
# 0.1.0 - November 3, 2013
4444

4545
* Add `Window` and `$window` alias.
4646

4747
* Support `Zepto` as well as `jQuery`.
4848

4949
* `Event` is now a wrapper around native event from dom listeners.
5050

51-
## 0.0.9 2013-06-15
51+
# 0.0.9 - June 15, 2013
5252

5353
* Revert earlier commit, and use `$document` as reference to jquery
5454
wrapped `document`.
@@ -57,13 +57,13 @@
5757

5858
* Depreceate $document.title and $document.title=.
5959

60-
## 0.0.8 2013-05-02
60+
# 0.0.8 - May 2, 2013
6161

6262
* Depreceate Document in favor of $document global.
6363

6464
* Add HTTP.delete() for creating DELETE request.
6565

66-
## 0.0.7 2013-02-20
66+
# 0.0.7 - February 20, 2013
6767

6868
* Add Element#method_missing which forwards missing calls to try and call
6969
method as a native jquery function/plugin.

‎README.md

+239-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# opal-jquery
1+
# opal-jquery: jQuery Wrapper For Opal
22

33
[![Build Status](http://img.shields.io/travis/opal/opal-jquery/master.svg)](http://travis-ci.org/opal/opal-jquery)
44

@@ -53,6 +53,243 @@ opal-jquery also supports zepto. To run specs for zepto use the rake task:
5353

5454
$ bundle exec rake zepto
5555

56+
## Getting Started
57+
58+
### Usage
59+
60+
`opal-jquery` can now be easily added to your opal application sources using a
61+
standard require:
62+
63+
```ruby
64+
# app/application.rb
65+
require 'opal'
66+
require 'jquery'
67+
require 'opal-jquery'
68+
69+
alert "Hello from jquery + opal"
70+
```
71+
72+
> **Note**: this file requires two important dependencies, `jquery` and `opal-jquery`.
73+
> You need to bring your own `jquery.js` file as the gem does not include one. If
74+
> you are using the asset pipeline with rails, then this should be available
75+
> already, otherwise download a copy and place it into `app/` or whichever directory
76+
> you are compiling assets from. You can alternatively require a zepto instance.
77+
78+
The `#alert` method is provided by `opal-jquery`. If the message displays, then
79+
`jquery` support should be working.
80+
81+
### How does opal-jquery work
82+
83+
`opal-jquery` provides an `Element` class, whose instances are toll-free
84+
bridged instances of jquery objects. Just like ruby arrays are just javascript
85+
arrays, `Element` instances are just jquery objects. This makes interaction
86+
with jquery plugins much easier.
87+
88+
Also, `Element` will try to bridge with Zepto if it cannot find jQuery loaded,
89+
making it ideal for mobile applications as well.
90+
91+
## Interacting with the DOM
92+
93+
### Finding Elements
94+
95+
opal-jquery provides the `Element` class, which can be used to find elements in
96+
the current document:
97+
98+
```ruby
99+
Element.find('#header')
100+
```
101+
102+
`Element.find` is aliased to `Element[]`:
103+
104+
```ruby
105+
Element['.my-class']
106+
```
107+
108+
These methods acts just like `$('selector')`, and can use any jQuery
109+
compatible selector:
110+
111+
```ruby
112+
Element.find('#navigation li:last')
113+
```
114+
115+
The result is just a jQuery instance, which is toll-free bridged to
116+
instances of the `Element` class in ruby:
117+
118+
```ruby
119+
Element.find('.foo').class
120+
# => Element
121+
```
122+
123+
Instances of `Element` also have the `#find` method available for
124+
finding elements within the scope of each DOM node represented by
125+
the instance:
126+
127+
```ruby
128+
el = Element.find('#header')
129+
el.find '.foo'
130+
# => #<Element .... >
131+
```
132+
133+
### Running code on document ready
134+
135+
Just like jQuery, opal-jquery requires the document to be ready to
136+
be able to fully interact with the page. Any top level access should
137+
use the `ready?` method:
138+
139+
```ruby
140+
Document.ready? do
141+
alert "document is ready to go!"
142+
end
143+
```
144+
145+
The `Kernel#alert` method is shown above too.
146+
147+
### Event handling
148+
149+
The `Element#on` method is used to attach event handlers to elements:
150+
151+
```ruby
152+
Element.find('#header').on :click do
153+
puts "The header was clicked!"
154+
end
155+
```
156+
157+
Selectors can also be passed as a second argument to handle events
158+
on certain children:
159+
160+
```ruby
161+
Element.find('#header').on(:click, '.foo') do
162+
puts "An element with a 'foo' class was clicked"
163+
end
164+
```
165+
166+
An `Event` instance is optionally passed to block handlers as well,
167+
which is toll-free bridged to jquery events:
168+
169+
```ruby
170+
Element.find('#my_link').on(:click) do |evt|
171+
evt.stop_propagation
172+
puts "stopped the event!"
173+
end
174+
```
175+
176+
You can access the element which triggered the event by `#current_target`.
177+
178+
```ruby
179+
Document.on :click do |evt|
180+
puts "clicked on: #{evt.current_target}"
181+
end
182+
```
183+
184+
### CSS styles and classnames
185+
186+
The various jQuery methods are available on `Element` instances:
187+
188+
```ruby
189+
foo = Element.find('.foo')
190+
191+
foo.add_class 'blue'
192+
foo.remove_class 'foo'
193+
foo.toggle_class 'selected'
194+
```
195+
196+
There are also added convenience methods for opal-jquery:
197+
198+
```ruby
199+
foo = Element.find('#header')
200+
201+
foo.class_name
202+
# => 'red lorry'
203+
204+
foo.class_name = 'yellow house'
205+
206+
foo.class_name
207+
# => 'yellow house'
208+
```
209+
210+
`Element#css` also exists for getting/setting css styles:
211+
212+
```ruby
213+
el = Element.find('#container')
214+
el.css 'color', 'blue'
215+
el.css 'color'
216+
# => 'blue'
217+
```
218+
219+
## HTTP/AJAX requests
220+
221+
jQuery's Ajax implementation is also wrapped in the top level HTTP
222+
class.
223+
224+
```ruby
225+
HTTP.get("/users/1.json") do |response|
226+
puts response.body
227+
# => "{\"name\": \"Adam Beynon\"}"
228+
end
229+
```
230+
231+
The block passed to this method is used as the handler when the request
232+
succeeds, as well as when it fails. To determine whether the request
233+
was successful, use the `ok?` method:
234+
235+
```ruby
236+
HTTP.get("/users/2.json") do |response|
237+
if response.ok?
238+
alert "successful!"
239+
else
240+
alert "request failed :("
241+
end
242+
end
243+
```
244+
245+
It is also possible to use a different handler for each case:
246+
247+
```ruby
248+
request = HTTP.get("/users/3.json")
249+
250+
request.callback {
251+
puts "Request worked!"
252+
}
253+
254+
request.errback {
255+
puts "Request didn't work :("
256+
}
257+
```
258+
259+
The request is actually triggered inside the `HTTP.get` method, but due
260+
to the async nature of the request, the callback and errback handlers can
261+
be added anytime before the request returns.
262+
263+
### Handling responses
264+
265+
Web apps deal with JSON responses quite frequently, so there is a useful
266+
`#json` helper method to get the JSON content from a request:
267+
268+
```ruby
269+
HTTP.get("/users.json") do |response|
270+
puts response.body
271+
puts response.json
272+
end
273+
274+
# => "[{\"name\": \"Adam\"},{\"name\": \"Ben\"}]"
275+
# => [{"name" => "Adam"}, {"name" => "Ben"}]
276+
```
277+
278+
The `#body` method will always return the raw response string.
279+
280+
If an error is encountered, then the `#status_code` method will hold the
281+
specific error code from the underlying request:
282+
283+
```ruby
284+
request = HTTP.get("/users/3.json")
285+
286+
request.callback { puts "it worked!" }
287+
288+
request.errback { |response|
289+
puts "failed with status #{response.status_code}"
290+
}
291+
```
292+
56293
## License
57294

58295
(The MIT License)
@@ -76,3 +313,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
76313
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
77314
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
78315
THE SOFTWARE.
316+

‎opal/opal-jquery/http.rb

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
require 'promise'
44
require 'opal-jquery/constants'
55

6-
# {HTTP} is used to perform +XMLHttpRequest+s in ruby. It is a simple wrapper
7-
# around jQuerys' +$.ajax+ call. +XMLHttpRequest+ is not wrapped directly as
6+
# {HTTP} is used to perform a `XMLHttpRequest` in ruby. It is a simple wrapper
7+
# around jQuerys' `$.ajax` call. `XMLHttpRequest` is not wrapped directly as
88
# jquery provides some cross browser fixes.
99
#
10-
# = Making requests
10+
# # Making requests
1111
#
1212
# To create a simple request, {HTTP} exposes class level methods to specify
1313
# the HTTP action you wish to perform. Each action accepts the url for the
@@ -16,7 +16,7 @@
1616
# HTTP.get("/users/1.json")
1717
# HTTP.post("/users", payload: data)
1818
#
19-
# The supported +HTTP+ actions are:
19+
# The supported `HTTP` actions are:
2020
#
2121
# * {HTTP.get}
2222
# * {HTTP.post}
@@ -25,12 +25,12 @@
2525
# * {HTTP.patch}
2626
# * {HTTP.head}
2727
#
28-
# = Handling responses
28+
# # Handling responses
2929
#
3030
# Responses can be handled using either a simple block callback, or using a
3131
# {Promise} returned by the request.
3232
#
33-
# == Using a block
33+
# ## Using a block
3434
#
3535
# All HTTP action methods accept a block which can be used as a simple
3636
# handler for the request. The block will be called for both successful as well
@@ -40,8 +40,8 @@
4040
# puts "the request has completed!"
4141
# end
4242
#
43-
# This +request+ object will simply be the instance of the {HTTP} class which
44-
# wraps the native +XMLHttpRequest+. {HTTP#ok?} can be used to quickly determine
43+
# This `request` object will simply be the instance of the {HTTP} class which
44+
# wraps the native `XMLHttpRequest`. {HTTP#ok?} can be used to quickly determine
4545
# if the request was successful.
4646
#
4747
# HTTP.get("/users/1") do |request|
@@ -54,7 +54,7 @@
5454
#
5555
# The {HTTP} instance will always be the only object passed to the block.
5656
#
57-
# == Using a Promise
57+
# ## Using a Promise
5858
#
5959
# If no block is given to one of the action methods, then a {Promise} is
6060
# returned instead. See the standard library for more information on Promises.
@@ -68,12 +68,12 @@
6868
# When using a {Promise}, both success and failure handlers will be passed the
6969
# {HTTP} instance.
7070
#
71-
# = Accessing Response Data
71+
# # Accessing Response Data
7272
#
7373
# All data returned from an HTTP request can be accessed via the {HTTP} object
7474
# passed into the block or promise handlers.
7575
#
76-
# * {#ok?} - returns +true+ or +false+, if request was a success (or not).
76+
# * {#ok?} - returns `true` or `false`, if request was a success (or not).
7777
# * {#body} - returns the raw text response of the request
7878
# * {#status_code} - returns the raw {HTTP} status code as integer
7979
# * {#json} - tries to convert the body response into a JSON object
@@ -92,7 +92,7 @@ class HTTP
9292

9393
# @!method self.get(url, options = {}, &block)
9494
#
95-
# Create a {HTTP} +get+ request.
95+
# Create a {HTTP} `get` request.
9696
#
9797
# @example
9898
# HTTP.get("/foo") do |req|
@@ -106,8 +106,8 @@ class HTTP
106106

107107
# @!method self.post(url, options = {}, &block)
108108
#
109-
# Create a {HTTP} +post+ request. Post data can be supplied using the
110-
# +payload+ options. Usually this will be a hash which will get serialized
109+
# Create a {HTTP} `post` request. Post data can be supplied using the
110+
# `payload` options. Usually this will be a hash which will get serialized
111111
# into a native javascript object.
112112
#
113113
# @example

0 commit comments

Comments
 (0)
Please sign in to comment.