在我的应用中,<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/