Skip to content

Commit

Permalink
run_node: run a D3.t on any Dom.node
Browse files Browse the repository at this point in the history
The previous interface for the run operation was too restrictive, as it
required D3 operations to run on trees that were attached to the
document. With this interface, it is now possible to run D3 operations
on trees that are not attached, for example those that have been
generated using tyxml.

Related to #1, #2.
  • Loading branch information
seliopou committed Oct 8, 2015
1 parent 4677b39 commit 032d09b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
5 changes: 3 additions & 2 deletions examples/rectangle.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ let view dims padding =
|. int attr "x" padding
|. int attr "y" padding
in
svg <.> rect
select "body"
|. svg <.> rect
;;

let _ =
run "body" () (view { width = 300; height = 300 } 20)
run () (view { width = 300; height = 300 } 20)
10 changes: 5 additions & 5 deletions examples/todo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ module View = struct
|. html (fun _ _ _ -> content)

let make k =
seq [todoapp k; low_footer]
nest (select "body")
[todoapp k; low_footer]
end

module Storage : sig
Expand Down Expand Up @@ -297,7 +298,7 @@ let main_lazy () =
* the code above without lwt, which will improve performance by a bit. *)
let model = ref { Model.init with Model.items = Storage.get () } in
let rec go () =
D3.run "body" !model (Lazy.force view)
D3.run !model (Lazy.force view)
and view = lazy (View.make (fun e ->
model := Event.handle e !model;
Storage.set (!model).Model.items;
Expand All @@ -313,15 +314,14 @@ let main_lwt () =
in
let view = View.make push in
let init = { Model.init with Model.items = Storage.get () } in
D3.run "body" init view;
D3.run init view;
Lwt_stream.fold (fun e m ->
let m' = Event.handle e m in
Storage.set m'.Model.items;
D3.run "body" m' view;
D3.run m' view;
m')
stream init
;;


let _ =
Lwt_js_events.async main_lwt
4 changes: 2 additions & 2 deletions lib/d3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ module E = struct
let handle name f = _handler name f
end

let run selector data t =
let run ?(node=Js.Unsafe.global##document) data t =
let cxt =
let open Js.Unsafe in
meth_call
(d3_select (Js.string selector))
(d3_select node)
"datum" [| inject data |]
in
ignore (t cxt)
Expand Down
15 changes: 9 additions & 6 deletions lib/d3.mli
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,14 @@ module E : sig
val handle : string -> (Dom_html.event, 'a) handler -> ('a, 'a) t
end

val run : string -> 'a -> ('a, _) t -> unit
(** [run selector datum op] selects the first element that matches [selector],
binds [datum] to it, and runs [op] on that element. This is the only way to
run a D3.t operation. It can be used in a variety of contexts, however its
indended use is in an event loop of an application, along these lines:
val run : ?node:Dom.node Js.t -> 'a -> ('a, _) t -> unit
(** [run ?node datum op] binds [datum] to [node] and runs [op] on that element.
If [node] is not provided, then the current document node will be used
instead.
This is the only way to run a D3.t operation. It can be used in a variety
of contexts, however its indended use is in an event loop of an
application, along these lines:
{[
let main () =
Expand All @@ -313,4 +316,4 @@ let main () =
D3.run "body" !model view)
;;
main ()]}*)
main ()]} *)

0 comments on commit 032d09b

Please sign in to comment.