Fla*_*pes 27 javascript ember.js
我有这个观点
App.ApplicationView = Em.View.extend({
templateName: 'application',
actions: {
myAction: function() {
//
}
}
});
Run Code Online (Sandbox Code Playgroud)
假设我想从另一个视图方法手动触发该操作,例如didInsertElement,如:
App.ApplicationView = Em.View.extend({
templateName: 'application',
actions: {
sidebarShowHome: function() {
this.set('sidebarIsHome', true);
this.set('sidebarIsNotifications', false);
this.set('sidebarIsArchive', false);
},
},
didInsertElement: function() {
this.actions.sidebarShowHome();
}
});
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?this.actions在视图方法中是未定义的.
int*_*xel 23
您可以使用TargetActionSupportmixin以及this.triggerAction:
App.ApplicationView = Ember.View.extend(Ember.TargetActionSupport, {
templateName: 'application',
actions: {
myAction: function() {
console.log('myAction');
}
},
didInsertElement: function() {
this.triggerAction({
action:'myAction',
target: this
});
}
});
Run Code Online (Sandbox Code Playgroud)
默认情况下,使用this.triggerAction()不带参数会将操作发送到控制器,但是如果需要定位不同的目标,请定义该target选项,如示例所示.
工作演示.
希望能帮助到你.
Nel*_*elu 14
要在同一个控制器中调用操作,您可以使用send.
this.send('nameOfAction', someParams);
Run Code Online (Sandbox Code Playgroud)
从组件使用中调用父控制器中的操作sendAction.
this.sendAction('nameOfAction', someParams);
Run Code Online (Sandbox Code Playgroud)
van*_*ome 12
triggerAction()如果视图使用Ember.TargetActionSupportmixin ,则该方法仅存在.如果不是这种情况,请this.get('controller').send('action');改用.
| 归档时间: |
|
| 查看次数: |
15275 次 |
| 最近记录: |