Module: Browser::DocumentMethods
- Defined in:
- lib/opal/jquery/document.rb
Overview
Document includes these methods to extend Element.
Generally, you will want to use the Document top level instance of Element.
Usage
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. 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
convenience, these are both exposed as #head and #body respectively,
and are just instances of Element.
puts Document.head
puts Document.body
# => #<Element: [<head>]>
# => #<Element: [<body>]>
Events
Document instances also have #on, #off and #trigger methods for
handling events. These all just delegate to their respective methods on
Element, using document as the context.
Document.on :click do |evt|
puts "someone clicked somewhere in the document"
end
Class Method Summary collapse
-
.ready?(&block) ⇒ Boolean
Register a block to run once the document/page is ready.
Instance Method Summary collapse
-
#body ⇒ Element
Element instance wrapping
document.body. -
#head ⇒ Element
Element instance wrapping
document.head. -
#ready ⇒ Object
Return a promise that resolves when the document is ready.
-
#title ⇒ String
Returns document title.
-
#title=(title) ⇒ Object
Set document title.
Class Method Details
.ready?(&block) ⇒ Boolean
Register a block to run once the document/page is ready. will call the block if the document is already ready
63 64 65 |
# File 'lib/opal/jquery/document.rb', line 63 def ready?(&block) @@__isReady ? block.call : `$(#{block})` if block_given? end |
Instance Method Details
#body ⇒ Element
Element instance wrapping document.body.
108 109 110 |
# File 'lib/opal/jquery/document.rb', line 108 def body Element.find `document.body` end |
#head ⇒ Element
Element instance wrapping document.head.
101 102 103 |
# File 'lib/opal/jquery/document.rb', line 101 def head Element.find `document.head` end |
#ready ⇒ Object
Return a promise that resolves when the document is ready.
74 75 76 77 78 |
# File 'lib/opal/jquery/document.rb', line 74 def ready promise = Promise.new Document.ready? { promise.resolve } promise end |
#title ⇒ String
Returns document title.
87 88 89 |
# File 'lib/opal/jquery/document.rb', line 87 def title `document.title` end |
#title=(title) ⇒ Object
Set document title.
94 95 96 |
# File 'lib/opal/jquery/document.rb', line 94 def title=(title) `document.title = title` end |