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

后端 未结 8 498
臣服心动
臣服心动 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 11:01

    It is possible, and yes, you can do it without usage of another another another tool, just using ember.js and nothing else. i did it like this:

    1) html code. note that all handlebars files need to be loaded before using any of them. here, its just one file named handlebars.js

    
      
      
          
      
      
      
    

    2) this file handlebars.js contains the following, using the compiler of ember

    var src = "Hello, {{name}}";
    Em.TEMPLATES["say-hello"] = Em.Handlebars.compile(src);
    

    3) and then inside your app.js file, just use it as it were available (which it is):

    var hu = Em.View.create({
        templateName: "say-hello",
        name: "Allô", 
        mouseDown: function() {  
        window.alert("hello world!");
    
      }
    });
    
    hu.appendTo("body");  
    

提交回复
热议问题