Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #700. onRendered event #705

Merged
merged 2 commits into from
Jul 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
}
};
});