Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax动态加载Ember模板_Javascript_Jquery_Ajax_Ember.js - Fatal编程技术网

Javascript 使用ajax动态加载Ember模板

Javascript 使用ajax动态加载Ember模板,javascript,jquery,ajax,ember.js,Javascript,Jquery,Ajax,Ember.js,我想根据需要使用以下代码从另一个html文件加载html: App.MainView = Ember.View.extend({ template:function(){ $.ajax({ url: 'views/main.html', dataType: 'text', async: false, success: function (res) { returnnow(res); } }

我想根据需要使用以下代码从另一个html文件加载html:

App.MainView = Ember.View.extend({
  template:function(){
    $.ajax({
       url: 'views/main.html',
       dataType: 'text',
       async: false,
       success: function (res) {
         returnnow(res);
       }
    });
    function returnnow(res){
      return Ember.Handlebars.compile(res);
    }
  }
});
但是它什么也不返回。。可能是由于ajax的异步特性

我不能用Ajax包装视图,因为我首先要在调用视图时加载它


你知道怎么做吗

您什么也得不到,因为您的
模板
函数没有返回任何内容。您需要像这样更改代码:

App.MainView = Ember.View.extend({
    template: function(){
       var result;
       $.ajax({
            url: 'views/main.html',
            success: function(data) {
                result = Ember.Handlebars.compile(data);
            }
       });
       return result;
    }
});

不要使用url:'views/main.html'尝试url:'views/main.php'。此外,Ember.handlebar.compile也存在一些问题。即使你得到了一个编译后的回报。你将无法渲染它。有一个错误称为“无法推动或其他”。但是我很确定你不能用html来做ajax响应。ajax加载本身工作得很好,我仔细检查了这一点。问题是返回的范围是函数范围。。但我现在没有更好的主意。也许
Ember.RSVP.Promise
因为Ajax是异步工作的,所以你的代码不会产生任何东西,如果我设置async:false,我得到了这个:
function(c,f){f=f |{};var g,h,I=f.partial?f:b;f.partial |(g=f.helpers,h=f.partials);var j=a.call(e,I,c,g,h,f.data);返回f.partial | d(e.compilerInfo),j;