Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // You create the trait.
- trait JavaScriptCompilerComponent {
- def compiler: JavaScriptCompiler
- trait JavaScriptCompiler {
- def compile(files: Seq[Source]): String
- }
- }
- // You implement it.
- trait DummyJavaScriptParserComponent extends JavaScriptCompilerComponent {
- override val compiler = new DummyJavaScriptCompiler
- class DummyJavaScriptCompiler extends JavaScriptCompiler {
- override def compile(files: Seq[Source]) = {
- ...
- }
- }
- }
- trait PageRenderer {
- def render: Elem
- }
- // You add the dependecy.
- class MyPageRenderer extends PageRenderer { this: JavaScriptCompilerComponent =>
- val jsFiles = Source.fromFile("test.js") :: Nil
- override def render =
- <html>
- <script>
- {compiler.compile(jsFiles)}
- </script>
- </html>
- }
- // And you inject it.
- val page = new MyPageRenderer with DummyJavaScriptParserComponent
- ...
- page.render
Add Comment
Please, Sign In to add comment