I recently switched over to using Webpack in my Aurelia application that I have been building at my day job. One of the caveats I encountered as a result of ditching Jspm/System.js is the ability of being able to supply a URL to getViewStrategy
because Webpack requires knowing all paths at bundle time, this approach goes out the window.
But, I recently encountered something cool that allows you to request HTML from the server and then use it as a view within your current custom component or wherever using InlineViewStrategy
. Essentially this gives you the ability to take arbitary HTML wrapped in <template></template>
tags and use it as a view.
Firstly, why would I want to do this? I am working within the confines of a .NET application. Some parts of the Aurelia application are server-side generated .cshtml
partials for reasons I will not go into. I need to get some HTML and then use it as a view within my application. Previously I could do the following.
Viewmodel:
export class MyCustomElement {
view: any = '';
constructor() {
this.view = '/Controller/partialName';
}
}
View:
<template>
<compose view.bind="view"></compose>
</template>
But if you switch over to Webpack you lose the ability to dynamically load views from a .NET controller or whatever, but I came up with a way to do it using a standard XMLHttpRequest and InlineViewStrategy.
As you will see below, we make a simple HTTP request to our server which is sending back HTML wrapped in <template>
tags.
I could have just created a custom loader for use with Webpack to handle loading dynamic content, but it seemed like just doing it using an InlineViewStrategy
achieved the same thing and was more straightforward. If I do create a loader, you’ll hear about it.
I’m just now starting down the same road as you with ASP.NET Core, Typescript, and webpack. I would have hit this issue almost immediately, so I’m glad you’ve already solved it :). I’m thinking of using your typescript-webpack-starter as reference https://github.com/Vheissu/aurelia-typescript-webpack-starter as there isn’t an ASP.NET Core based typescript and webpack example yet. Thanks for this post.
I have a question.
Is it possible by the same way (InlineViewStrategy), to render html pages with composite UI of jQery components and events ? e.g a page with many Syncfusion widgets,fetching their data from the back-end REST API ?
Something like rendering them as iframes , only Aurelia to handle the routing part ?
This will give us, a lot of flexibility, till the component providers prepare their bindings for Aurelia.
thank you
Stefan
Thanks, Dwayne.
What other drawback have you run into because of the switch from the jspm? Or is this the only one? Is is still worth switching to webpack?
Thanks in advance.
Nadya