Skip to content

Commit

Permalink
Merge pull request #705 from varya/feature/700_onRendered
Browse files Browse the repository at this point in the history
Fix #700. onRendered event
  • Loading branch information
hannu committed Jul 14, 2015
2 parents 361c406 + 4ee0390 commit db0e9fe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,52 @@ should be added to the other styles when building:
.pipe(gulp.dest(outputPath));
});

### Providing additional JavaScript

To provide additional JavaScript for the StyleGuide pages, define its `<script>` tas in the `extraHead` parameter:


gulp.task('styleguide:generate', function() {
return gulp.src('*.scss')
.pipe(styleguide.generate({
...
extraHead: [
'<script src="/path/to/my-js-file.js"></script>'
],
disableEncapsulation: true
...
}))
.pipe(gulp.dest(outputPath));
});


Include other needed scripts, such as libraries, into the same array:

extraHead: [
'<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>',
'<script src="/path/to/my-js-file.js"></script>'
]

This way you can enrich the documented components with JavaScript. Keep in mind that you need to use `disableEncapsulation` parameter to make the components visible for the parent page JavaScript (otherwise they are encapsulated with shadowDOM).

### onRendered event

The components get visible onto the StyleGuide pages dynamically. This means that it takes some time to render them.

In your JavaScript you need to operate components after they had been rendered. Catch `styleguide:onRendered` event on `window` for that:

$(window).bind("styleguide:onRendered", function(e) {
// do anything here
// use e.originalEvent.detail.elements to get elements
});

This is useful when you need to initialize your components. As this kind of initializing is only needed on the StyleGuide pages, you can provide it with an additional file:

extraHead: [
'<script src="/path/to/my-js-file.js"></script>',
'<script src="/js/init-styleguide.js"></script>'
]

## Demo

Build demo style guide and start a server on port 3000
Expand Down
12 changes: 12 additions & 0 deletions lib/app/js/directives/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ angular.module('sgApp')
$timeout(function() {
updateCurrentReference();
});

// Emit an event that an element is rendered
element.ready(function() {
var event = new CustomEvent('styleguide:onRendered', {
detail: {
elements: element
},
bubbles: true,
cancelable: true
});
$window.dispatchEvent(event);
});
}
};
});

0 comments on commit db0e9fe

Please sign in to comment.