2013-07-22 34 views
2

我想从服务器上传ember模板。 我看到对于那些需要使用的,如:灰烬。实时上传模板

$.ajax({ 
    url: 'url_to_template_text', 
    dataType: 'text', 
    success: function (resp) { 
     App.AboutView = Ember.View.extend({ 
      template: Ember.Handlebars.compile(resp) 
     }); 
    } 
}); 

但我无法理解如何渲染页面上这一观点。 App.AboutView.append(中) - 不工作

如果添加路由的这一观点,那么就没有时间来呈现越来越模板:

<script type="text/x-handlebars" > 
    {{outlet}} 
</script> 

<script type="text/x-handlebars" data-template-name="about"> 
    That text cant be show 
</script> 

//////JS 

$.ajax({ 
    url: 'url_to_template_text', 
    dataType: 'text', 
    success: function (resp) { 

     App.AboutView = Ember.View.extend({ 
      templateName: 'about', 
      template: Ember.Handlebars.compile(resp) 
     }); 

    } 
}); 


App.Router.map(function() { 
    this.route("about", { path: "/" }); 
}); 

没有奏效过。渲染最古老的模板内容(我的意思是“那个文本不能显示”)

请帮助我,也许我用坏的方式?

回答

3

您可以使用beforeModel挂钩在model挂钩旁边加载模板。在这种情况下,您似乎也想使用它作为路由的默认视图进行解析。您可以使用灰烬约定做到这一点,AboutRoute - > AboutView - > AboutController等

beforeModel: function() { 
    return $.ajax({ 
    url: '/about.hbs' 
    }) 
    .then(function(response) { 
    Em.TEMPLATES.about = Em.Handlebars.compile(response); 
    }); 
}, 

后,您加载你需要把它分配给了全球Ember.TEMPLATES对象的模板。

另一种方法是对视图模板执行相同操作。通过重新打开视图的类并添加加载的模板,如上所述。请注意,您仍然必须使用{{view App.MyView}}的手柄模板中的视图。

这里是一个jsbin的例子。

+0

谢谢,它为我工作 –