Skip to content

Commit

Permalink
Merge pull request #76 from opal/ready-promise
Browse files Browse the repository at this point in the history
merges / fixes #64
elia committed Jul 31, 2015
2 parents a439450 + dc644be commit f70f8e3
Showing 3 changed files with 65 additions and 4 deletions.
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)

0 comments on commit f70f8e3

Please sign in to comment.