require.js text plugin adds “.js” to the file name

后端 未结 7 1078
旧时难觅i
旧时难觅i 2021-01-08 00:31

I\'m trying to work with requirejs and text plugin and I have weird problem.

I have two web servers:

  1. localhost:3000 - act as CDN and h
7条回答
  •  爱一瞬间的悲伤
    2021-01-08 00:46

    As another alternative way you can use jQuery get method to fetch the content of the template as plain text and then compile it with Handlebars. I came to this solution as a last resort after spending several hours into forums reading about issues with require.js text plugin and CORS. Here's an example :

    The template :

    Default
    

    {{title}}

    {{body}}

    The js script :

    var deps = [
        'jquery',
        'handlebars',
    ];
    
    require(deps, function($, handlebars){
    
        var template = 'https://your_site/raw.templates/basic.handlebars';
    
        $.get(template, function( data, textStatus, jqxhr ) {
            var Handlebars = handlebars;
    
            var basicTemplate = Handlebars.compile(data);
    
            var context = {title: "My New Post", body: "This is my first post!"};
    
            var html = basicTemplate(context);
    
            var grid = document.createElement('div');
            grid.setAttribute('id', 'itemsGrid');
            grid.innerHTML = html;
            document.body.appendChild(grid);
    
        });
    
    });
    

提交回复
热议问题