Is it possible to load handlebar template with script tag? Or define handlebar templates programmatically in Ember.js

后端 未结 8 516
臣服心动
臣服心动 2021-01-31 10:38

Simply enough I do not want to define all my handlebar templates in my html file

I tried this



        
8条回答
  •  情话喂你
    2021-01-31 10:43

    So since I still wanted separate files for my templates and I didn't want to define them in strings in the javascript I hacked this together last night

    It is a synchronous lazy loader, it loads all the templates first, then ember and the rest of my code,

            //fake function so that every loads fine will get redefined in application.js
            function initializeApp(){}
    
            function loadTemplates(){
                var arg = arguments[0],
                    next = Array.prototype.slice.call(arguments,1);
                if(typeof arg != 'string'){
                    arg()
                }else{
                    var scriptObj = document.createElement('script');
                    scriptObj.type = 'text/x-handlebars';
                    $(scriptObj).attr('data-template-name', arg.replace('.handlebars', '').substring(arg.lastIndexOf('/')+1))
                    $.get(arg, function(data){
                        scriptObj.text = data;
                        document.head.appendChild(scriptObj);
                        if(next.length > 0) loadTemplates.apply(this, next);
                    });
                }
            }
    
            function loadScripts() {
                var script = arguments[0],
                    scriptObj = document.createElement('script'),
                    next = Array.prototype.slice.call(arguments,1);
                scriptObj.type = 'text/javascript';
                scriptObj.src = script;
                scriptObj.onload = scriptObj.onreadystatechange = (next.length > 0) ? function(){loadScripts.apply(this, next)} : function(){$(document).ready(function() {initializeApp()})};
                document.head.appendChild(scriptObj);
            }
    
            function loadApp(obj){
                loadTemplates.apply(this, obj.templates.concat(function(){loadScripts.apply(this,obj.scripts)}))
            }
    
            window.onload = function(){
                loadApp({
                    templates:
                        [
                            '/javascripts/views/templates/nav-bar.handlebars',
                        ],
                    scripts:
                        [
                            'https://maps.googleapis.com/maps/api/js?sensor=false&callback=initializeGoogleMaps',
                            '/javascripts/lib/bootstrap.js', 
                            '/javascripts/lib/rails.js', 
                            '/javascripts/lib/ember.js',
                            '/javascripts/application.js',
                            '/javascripts/views/nav_bar.js',
                        ]
                })
            }
    

    EDIT: I cleaned it up and made it work properly only testing in chrome though

提交回复
热议问题