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

inlineKnockoutTemplates issues #81

Closed
Munter opened this issue Jun 5, 2013 · 2 comments
Closed

inlineKnockoutTemplates issues #81

Munter opened this issue Jun 5, 2013 · 2 comments

Comments

@Munter
Copy link
Member

Munter commented Jun 5, 2013

I'm using the following pattern. It might be an antipattern, so I'll accept arguments agains this pattern.

define([
    'tpl!myTpl.ko'
], function (tpl) {
    $(tpl).appendTo(document.body);
});

So my requirejs tpl plugin loads the template into knockout as an externaltemplate, but it also returns the template string for use in the amd module.

Now, when this runs through the inlineKnockoutTemplates transform, the templates are appended to the body, the require statements are gone, and the arguments get removed.

This causes a bit of a problem, which I have to work around like this:

define([
    'tpl!myTpl.ko'
], function (tpl) {
    var myTpl = typeof tpl !== 'undefined' ? tpl : $('#myTpl').html();
    $(myTpl).appendTo(document.body);
});

This is obviously a bit annoying, so I'd like input on changing this pattern.

Another Issue I just noticed is that the knockout templates get appended to the body as the last items. After the main javascript bundle. This triggered an error in IE for me where the template wasn't in the dom yet when the script ran. Is there a way to inline the templates at an earlier point, before the main javascript bundle?

@papandreou
Copy link
Member

So my requirejs tpl plugin loads the template into knockout as an externaltemplate, but it also returns the template string for use in the amd module.

Hmm, it sounds like you're using an old version of the tpl plugin. The newest version does approximately the same as buildProduction -- appends a <script type="text/html"> to <body> so knockout's regular templating engine knows where to find it. I just open sourced it here: https://github.com/One-com/requirejs-tpl/

Would updating to this version remove the need for you to get the actual contents as a JavaScript string?

Another Issue I just noticed is that the knockout templates get appended to the body as the last items. After the main javascript bundle. This triggered an error in IE for me where the template wasn't in the dom yet when the script ran. Is there a way to inline the templates at an earlier point, before the main javascript bundle?

Never ran into that, probably because I call ko.applyBindings after DOMContentLoaded. Would that be an option for you? It would be easy to add the templates at the start of <body>, of course, but then they might end up adding weight before the markup for a loading screen, so I'd rather not do that.

@Munter
Copy link
Member Author

Munter commented Sep 24, 2014

I'm no longer working on that code base, so I have no way to follow up on any fixes. Closing until someone else takes the issue up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants