Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal-jquery
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a43945039743
Choose a base ref
...
head repository: opal/opal-jquery
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: dc644be0a9d0
Choose a head ref
  • 5 commits
  • 3 files changed
  • 1 contributor

Commits on Jul 31, 2015

  1. closes #61 closes #62

    catmando authored and elia committed Jul 31, 2015
    Copy the full SHA
    6fb5178 View commit details
  2. updated readme

    catmando authored and elia committed Jul 31, 2015
    Copy the full SHA
    44ad88c View commit details
  3. Update README.md

    cleaned up explanation of Document.ready promise
    catmando authored and elia committed Jul 31, 2015
    Copy the full SHA
    fdd2755 View commit details
  4. removed unnecessary block param in spec

    catmando authored and elia committed Jul 31, 2015
    Copy the full SHA
    1fb1fce View commit details
  5. Update README.md

    explain how to add xhr option
    catmando authored and elia committed Jul 31, 2015
    Copy the full SHA
    dc644be View commit details
Showing with 65 additions and 4 deletions.
  1. +28 −1 README.md
  2. +28 −2 lib/opal/jquery/document.rb
  3. +9 −1 spec/document_spec.rb
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -142,7 +142,15 @@ Document.ready? do
end
```

The `Kernel#alert` method is shown above too.
or the equivilent `Document.ready` promise which is useful when combined with other promises:

```ruby
Document.ready.then do |ready|
alert "Page is ready to use!"
end
```

Notice the use of the `Kernel#alert` method.

### Event handling

@@ -290,6 +298,24 @@ request.errback { |response|
}
```

### Supplying an XHR method

To supply an XHR callback include a lambda with the `xhr` option:

```ruby
update_progress = lambda do
xhr = `new window.XMLHttpRequest()`
update_progress = lambda do |evt|
# update your progress here
end
`xhr.upload.addEventListener("progress", update_progress, false)`
xhr
end

cloud_xfer = HTTP.put "http://my.cloud.storage/location", xhr: update_progress, ... etc ...
```


## Usage of JQuery plugins
Extra plugins used for JQuery aren't available to ruby code by default, you will have to `expose` these functions to opal-jquery.

@@ -306,6 +332,7 @@ el = Element['.html_element']
el.cool_plugin({argument: 'value', argument1: 1000}.to_n)
```


## License

(The MIT License)
30 changes: 28 additions & 2 deletions lib/opal/jquery/document.rb
Original file line number Diff line number Diff line change
@@ -11,14 +11,22 @@ module Browser
#
# A useful method on {Document} is the {#ready?} method, which can be used to
# run a block once the document is ready. This is equivalent to passing a
# function to the `jQuery` constructor.
# function to the `jQuery` constructor. Unlike jQuery it will work correctly
# even if called *after* the document is already loaded.
#
# Document.ready? do
# puts "Page is ready to use!"
# end
#
# Just like jQuery, multiple blocks may be passed to {#ready?}.
#
# Document.ready (without the question mark) returns the equivilent promise.
# Like other promises it can be combined using the when and then methods.
#
# Document.ready.then do |ready|
# puts "Page is ready to use!"
# end
#
# ### Document head and body elements
#
# Every document has atleast two elements: a `head` and `body`. For
@@ -45,15 +53,33 @@ module DocumentMethods
`var $ = #{JQUERY_SELECTOR.to_n}` # cache $ for SPEED

# Register a block to run once the document/page is ready.
# will call the block if the document is already ready
#
# @example
# Document.ready? do
# puts "ready to go"
# end
#
def ready?(&block)
`$(#{block})` if block_given?
@@__isReady ? block.call : `$(#{block})` if block_given?
end

# Return a promise that resolves when the document is ready.
#
# @example
# Document.ready.then do |r|
# puts "ready to go"
# end
#
def ready
promise = Promise.new
Document.ready? { promise.resolve }
promise
end

module_function :ready?

ready? { @@__isReady = true }

# Returns document title.
#
10 changes: 9 additions & 1 deletion spec/document_spec.rb
Original file line number Diff line number Diff line change
@@ -8,7 +8,15 @@
Document.ready? { }
end
end


describe "ready" do
async "resolves when document is ready" do
Document.ready.then do
async { Document.ready.resolved?.should be_truthy }
end
end
end

describe "title" do
it "gets the document title" do
Document.title.should be_kind_of(String)