Currently, I am utilizing Webpack to transpile my ES6 classes. Within the bundle, there is a Service
class that can be imported by other bundled scripts.
class Service {
constructor() {
//
}
someMethod(data) {
//
}
}
export default Service;
Now, I have a small inline script in the HTML body (pseudo-code provided below), which needs to invoke a method in the Service
class with data inserted server-side via a template engine like Twig or Blade. Naturally, creating a new instance of the Service
won't suffice...
<body>
...
<script>
var data = {{ $json_server_data }};
var service = new Service;
Service.someMethod(data);
</script>
</body>
I am keen on having the server data readily available inline to avoid an extra asynchronous call. The idea of cluttering the window namespace with the Service
class seems counterproductive considering the advantages of a class loader...
How do you propose dealing with this? Any suggestions for alternative approaches are appreciated as well.