我的 index.html 有一个 posts
模板和一个 post
模板。帖子模板是一系列链接,这些链接将更改 post
模板中显示的帖子。我的一些帖子的内容需要使用一些 jQuery 进行调整。每次 post
模板中显示的帖子更改为不同的帖子时,如何运行 jQuery 代码?
最佳答案
假设您有一个 post 路由,每次您为其路由提供一个新模型时,setupController
都会运行。
App.PostRoute = Ember.Route.extend({
setupController: function(controller, model){
this._super(controller, model);
Ember.run.scheduleOnce('afterRender', function(){
alert('post has changed, and it\'s been rendered change away');
});
}
});
由于这通常让我感到有点内疚(在路由中添加 View 逻辑),您可以在 Controller 上观察模型(从 View 或 Controller ,每次模型更改都会触发类似的逻辑。您可能需要沿着这条路线节流它,我很确定它会发射两次,消隐它,然后将它设置为不同的项目。
App.PostView = Ember.View.extend({
watchModel: function(){
Ember.run.scheduleOnce('afterRender', function(){
alert('color has changed, and it\'s been rendered change away');
});
}.observes('controller.model')
});
关于javascript - 当 Ember 模板更改时运行 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23717786/