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

后端 未结 8 497
臣服心动
臣服心动 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:47

    You can also patch Ember View to load templates on get

    Em.View.reopen({
        templateForName: function(name, type) {
            if (!name) { return; }
    
            var templates = Em.get(this, 'templates'),
                template = Em.get(templates, name);
    
            if (!template) {
                $.ajax({
                    url: 'templates/%@.hbs'.fmt(name),
                    async: false
                }).success(function(data) {
                    template = Ember.Handlebars.compile(data);
                });
            }
    
            if (!template) {
                throw new Em.Error('%@ - Unable to find %@ "%@".'.fmt(this, type, name));
            }
    
            return template;
        }
    });
    

    UPDATE: Since Ember 1.0.0-pre.3 this solution probabaly no more work (maybe can be migrated to recent Ember)

提交回复
热议问题