页面模板中的操作交互按钮,默认指向该模板对应的控制器。当然也可以指向相对应的路由和视图,只需要在这些类里面定义该操作处理函数即可,例如
{{#if isExpanded}} <div class=‘body‘>{{body}}</div> <button {{action ‘contract‘}}>Contract</button> {{else}} <button {{action ‘expand‘}}>Show More...</button> {{/if}}
App.PostController = Ember.ObjectController.extend({ // initial value isExpanded: false, actions: { expand: function() { this.set(‘isExpanded‘, true); }, contract: function() { this.set(‘isExpanded‘, false); } } });
App.PostRoute = Ember.Route.extend({ actions: { expand: function() { this.controller.set(‘isExpanded‘, true); }, contract: function() { this.controller.set(‘isExpanded‘, false); } } });
App.PostsIndexView = Ember.View.extend({ actions: { select: function(post) { // do your business. } } });
时间: 2024-10-19 23:04:22