1

私のアプリでは、タグにはすべてのビューを含む<body>単一の<script type="text/x-handlebars>タグが含まれています。Sproutcore 2.0は、これらのテンプレートを解析してDOMにレンダリングするjQueryon-document-readyハンドラーをうまく追加します。

レンダリングされたらすぐに、ビューの1つで関数を呼び出したいのですが。問題は、再挿入が非同期で行われるため、ビューがいつ使用可能になるかわかりません。

ページ
<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();
  }
});

また、Sproutcoreがすべてのビューのレンダリングと挿入の後にそれを実行することを期待して試しSC.run.schedule('render', function() { MyApp.TweetInputView.instance.focus(); }, 'call');ましたが、そうではないようです。

4

1 に答える 1

5

これを試して:

MyApp.TweetInputView = SC.View.extend({
  didInsertElement: function() {
    console.log("I've been rendered!");
  }
});
于 2011-09-02T23:25:27.867 に答える