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

发布于 2024-12-02 18:07:56 字数 971 浏览 1 评论 0原文

在我的应用程序中, 标记仅包含一个

我想在其中一个视图渲染后立即调用一个函数。重新插入是异步发生的,所以我不知道视图何时可用。

示例

Page
<body>
  <script type="text/x-handlebars">
    ...
    {{view "MyApp.TweetInputView"}}
    ...
  </script>
</body>
View:
MyApp.TweetInputView = SC.View.extend({
  init: function() {
    // act like a singleton
    MyApp.TweetInputView.instance = this;
    return this._super();
  },
  focus: function() {
    ...
    this.$().focus();
  }
});
Initializer
// 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 在所有视图渲染和插入之后运行它,但情况似乎并非如此。

In my app, the <body> tag contains just a single <script type="text/x-handlebars> tag which contains all my views. Sproutcore 2.0 nicely adds a jQuery on-document-ready handler that parses those templates and renders them back into the DOM.

I'd like to call a function on one of the views as soon as it's rendered. The problem is that the re-insertion happens asynchronously, so I don't know when the view is available.

Example

Page

<body>
  <script type="text/x-handlebars">
    ...
    {{view "MyApp.TweetInputView"}}
    ...
  </script>
</body>

View:

MyApp.TweetInputView = SC.View.extend({
  init: function() {
    // act like a singleton
    MyApp.TweetInputView.instance = this;
    return this._super();
  },
  focus: function() {
    ...
    this.$().focus();
  }
});

Initializer

// 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();
  }
});

I've also tried SC.run.schedule('render', function() { MyApp.TweetInputView.instance.focus(); }, 'call'); in the hopes that Sproutcore would run that after all the view rendering and insertion, but that does not seem to be the case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま昔日黯然 2024-12-09 18:07:56

试试这个:

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

Try this:

MyApp.TweetInputView = SC.View.extend({
  didInsertElement: function() {
    console.log("I've been rendered!");
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文