Skip to content

Commit

Permalink
Added debugging BlazeComponentDebug.
Browse files Browse the repository at this point in the history
Now you can run `BlazeComponentDebug. dumpAllComponents()` and enjoy
debugging.
  • Loading branch information
mitar committed May 14, 2015
1 parent cde8400 commit 90c887d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ ES6](#javascript-and-es6-support).

See [live tutorial](http://components.meteor.com/) for an introduction.

Adding this package to your Meteor application adds `BlazeComponent` class into the global scope.
Adding this package to your Meteor application adds `BlazeComponent` and `BlazeComponentDebug` classes into the
global scope.

Client side only.

Expand All @@ -33,6 +34,7 @@ Client side only.
* [Utilities](#utilities)
* [Low-level DOM manipulation hooks](#low-level-dom-manipulation-hooks)
* [Mixins](#mixins-1)
* [Debugging](#debugging)
* [Related projects](#related-projects)

Installation
Expand Down Expand Up @@ -1401,6 +1403,39 @@ If mixin is already added to the component the method does nothing.
Use `requireMixin` to manually add additional mixins after a component was created. For example, to add
dependencies required by automatically added mixins as a result of [`mixins`](#user-content-reference_instance_mixins).

Debugging
---------

To help with debugging, `BlazeComponentDebug` class is available. It contains class methods which can help
you introspect the current state of rendered components.

<a name="debugging_class_dumpComponentSubtree"></a>
```coffee
@dumpComponentSubtree: (componentOrElement) ->
```

For a provided component instance or DOM element rendered by a component instance this class method prints
to the browser web console the tree structure of component instances for which the provided component instance
is an ancestor.

<a name="debugging_class_dumpComponentTree"></a>
```coffee
@dumpComponentTree: (componentOrElement) ->
```

For a provided component instance or DOM element rendered by a component instance this class method prints
to the browser web console the whole tree structure of component instances in which the provided component
instance exists, from the root component instance down.

The provided component instance's name is underlined.

<a name="debugging_class_dumpAllComponents"></a>
```coffee
@dumpAllComponents: ->
```

Prints to the browser web console tree structures of all component instances currently rendered.

Related projects
----------------

Expand Down
36 changes: 36 additions & 0 deletions debug.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class BlazeComponentDebug extends BaseComponentDebug
@startComponent: (component) ->
super

console.log component.data()

@startMarkedComponent: (component) ->
super

console.log component.data()

@dumpComponentSubtree: (rootComponentOrElement) ->
if rootComponentOrElement.nodeType is Node.ELEMENT_NODE
rootComponentOrElement = BlazeComponent.getComponentForElement rootComponentOrElement

super

@dumpComponentTree: (rootComponentOrElement) ->
if rootComponentOrElement.nodeType is Node.ELEMENT_NODE
rootComponentOrElement = BlazeComponent.getComponentForElement rootComponentOrElement

super

@dumpAllComponents: ->
allRootComponents = []

$('*').each (i, element) =>
component = BlazeComponent.getComponentForElement element
return unless component
rootComponent = @componentRoot component
allRootComponents.push rootComponent unless rootComponent in allRootComponents

for rootComponent in allRootComponents
@dumpComponentSubtree rootComponent

return
7 changes: 5 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Package.onUse(function (api) {

// Internal dependencies.
api.use([
'peerlibrary:base-component@0.8.0'
'peerlibrary:base-component@0.9.2'
]);

// 3rd party dependencies.
Expand All @@ -31,11 +31,14 @@ Package.onUse(function (api) {
]);

api.export('BlazeComponent');
// TODO: Move to a separate package. Possibly one with debugOnly set to true.
api.export('BlazeComponentDebug');

// Client.
api.addFiles([
'lookup.js',
'lib.coffee'
'lib.coffee',
'debug.coffee'
], 'client');
});

Expand Down

0 comments on commit 90c887d

Please sign in to comment.