dQuery

dQuery is a jQuery-like API for querying and manipulating your server-side DOM. It also stress-tests domonic’s JavaScript-like runtime and DOM implementation.

querying

dQuery uses the º symbol (alt+0).

from domonic.html import *
from domonic.dQuery import º

d = html(head(body(li(_class='things'), div(_id="test"))))

º(d) # you need to init a dom first. i.e. a html element

# now you can use it
print( º('#test') )
# <div id="test"></div>
print( º('.things') )
# <li class="things"></li>
a = º('<div class="test2"></div>')
print( a )
# <div class="test2"></div>

b = º('#test').append(a)
print(b)
# <div id="test"><div class="test2"></div></div>

You can quickly access returned elements as if they were a list:

somehtml = º('<html><table id="mytable" class="one"></table></html>')
str(º('table')[0])
# <table id="mytable" class="one"></table>

You do not need a DOM fragment to use dQuery. It also contains useful static methods:

first = ["a", "b", "c"]
second = ["d", "e", "f"]
result = º.merge(first, second)
print(result)
# ['a', 'b', 'c', 'd', 'e', 'f']

obj1 = {'a':1,'b':2}
obj2 = {'c':1,'b':5}
print(º.extend(obj1,obj2))
# {'a': 1, 'b': 5, 'c': 1}

print(º.trim("  some tst \n   TEST."))
# 'some tst    TEST.'

print(º.now())
# milliseconds since the epoch, e.g. 1788565840995

Ajax helpers

º.ajax(), º.get(), º.getJSON() and º.post() wrap requests with jQuery-like callbacks and global Ajax events.

from domonic.dQuery import º

º.ajaxStart(lambda event: print("loading"))
º.ajaxStop(lambda event: print("done"))

data = º.getJSON("https://example.com/api", {"q": "domonic"})
º.post("https://example.com/save", {"name": "Ada"})

Manipulate Nodes

from domonic.dQuery import º
from domonic.html import div, html, p

page = html(div(p("one"), _id="app"))
º(page)

º("#app").append("<p>two</p>")
º("#app p").addClass("item")

print(page)
# <html><div id="app"><p class="item">one</p><p class="item">two</p></div></html>