javascript - 如何等待 Sproutcore 2.0 加载所有模板?

标签 javascript sproutcore sproutcore-2

在我的应用中,<body>标签只包含一个 <script type="text/x-handlebars>包含我所有观点的标签。 Sproutcore 2.0 很好地添加了一个 jQuery on-document-ready 处理程序,用于解析这些模板并将它们呈现回 DOM。

我想在其中一个 View 呈现后立即调用一个函数。问题是重新插入是异步发生的,所以我不知道 View 何时可用。

例子

<body>
  <script type="text/x-handlebars">
    ...
    {{view "MyApp.TweetInputView"}}
    ...
  </script>
</body>
看法:
MyApp.TweetInputView = SC.View.extend({
  init: function() {
    // act like a singleton
    MyApp.TweetInputView.instance = this;
    return this._super();
  },
  focus: function() {
    ...
    this.$().focus();
  }
});
初始化程序
// if the URL is /tweets/new, focus on the tweet input view
$(function() {
  if (window.location.pathname === '/tweets/new') {
    // doesn't work, because the view hasn't been created yet:
    MyApp.TweetInputView.instance.focus();
  }
});

我也试过 SC.run.schedule('render', function() { MyApp.TweetInputView.instance.focus(); }, 'call');希望 Sproutcore 在所有 View 渲染和插入之后运行它,但事实似乎并非如此。

最佳答案

试试这个:

MyApp.TweetInputView = SC.View.extend({
  didInsertElement: function() {
    console.log("I've been rendered!");
  }
});

关于javascript - 如何等待 Sproutcore 2.0 加载所有模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7290094/

相关文章:

Javascript替换方法避免复制粘贴

javascript - SproutCore:在Buildfile中设置代理不起作用

sproutcore - [Help]如何在生产模式下运行 Sproutcore 项目?

javascript - 递归集合在 sproutcore2 中是否可行?

javascript - 如何让一个盒子粘在上面?

javascript - 比较当前日期和日期 inp

sproutcore - SproutCore命名空间

javascript - 检测数据存储中记录的更改

Javascript/AngularJS - 等待回调完成执行