Skip to content
jasononeil edited this page May 1, 2013 · 1 revision

Where possibly, Detox tries employ "Method Chaining", which is where each method (where sensible), returns the object you started with, so you can chain things together:

paragraph.addClass("text-center").setAttr("id", "my-paragraph").click(clickHandler).appendTo(body).setText("Hello World!");
// <p class="text-center" id="my-paragraph">Hello World!</p>
// The paragraph will be attached to <body>
// And when you click it, "clickHandler()" will fire

The exception to that rule is when the method is supposed to return something:

paragraph.hasClass("text-center"); 
// true

paragraph.attr("id");
// "my-paragraph"

paragraph.text();
// "Hello World!"

paragraph.parent();
// <body>

paragraph.children(false);
// DOMCollection: [ TextNode:"Hello World!" ]

In general, chaining Nodes and collections will return whatever you started with. So if you started with a Node, at the end of your chain will be another Node. If you start with a Collection, at the end of the chain will be a collection.

If you use Traversing methods, you may end up on a different set of elements:

ul.addClass("list");
// The end of the chain contains the ul that you started with

ul.addClass("list").children().addClass("list-item");
// The end of the chain contains the collection given by children()

Currently there is no way to step back to a previous collection in the chain in the way that jQuery's end() method does. If you feel that such a feature is worthwhile, post a feature request :)

Clone this wiki locally