延迟加载Ember View模板,直到完成AJAX请求

时间:2012-03-20 22:14:45

标签: ember.js

在Ember View的willInsertElement属性中从AJAX请求返回响应之前,看起来不可能延迟模板的加载?

我想在模板渲染之前加载jquery-ui.min.js。

window.App = Ember.Application.create()

App.TestView = Ember.View.create
    tagName: 'div'
    template: Ember.Handlebars.compile '<div>This is a view</div>'
    willInsertElement: ->
      $.ajax({
        url: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"
        dataType: "script"
        async: false
        success: (data, textStatus, jqxhr) ->
          console.log data
          console.log textStatus
          console.log jqxhr.status
          console.log "Load was performed."
      });
    didInsertElement: ->
      console.log 'the element was inserted'

App.TestView.append()

我发现didInsertElement在AJAX请求完成之前运行。

先谢谢。

2 个答案:

答案 0 :(得分:2)

你最终想要达到什么目的?确保在插入视图之前jquery-ui可用?

  • 您可以将视图添加到success回调中,然后参阅http://jsfiddle.net/RgcJA/
  • 我没有测试过你的代码,但$.getScript是异步执行的吗?您是否尝试过设置async: false

答案 1 :(得分:0)

您可以尝试将视图的visible属性绑定到变量,然后在文件上载完成后切换该变量。

您还必须在willInsertElement

之外开始上传文件
相关问题