gpt4 book ai didi

events - Marionette 布局 : trigger event on child view

转载 作者:行者123 更新时间:2023-12-04 02:44:08 24 4
gpt4 key购买 nike

我有一个布局 View ,里面有一个 itemView。我的项目 View 中有一个事件触发了保存功能。在该保存函数中,我想触发布局捕获的另一个事件。

所以在下面的代码中,在 onClickSave modelSaveSuccess 中我想在父布局中触发一个函数,我已经尝试过 this.methodInParent() 但它不起作用

subview

define(["marionette", "underscore", "text!app/templates/client/form.html", "app/models/client"], function(Marionette, _, Template, Model) {
"use strict"
return Backbone.Marionette.ItemView.extend({
events: {
"submit #saveClient": "onClickSave"
},
onClickSave: function(ev) {
ev.preventDefault()
return this.model.save({}, {
success: function() {
console.log('success - trigger ')
},
error: function(request, error) {
console.log(error.responseText)
}
})
}
})
})

最佳答案

一个不引入重耦合的好方法是使用 Marionette 的 event aggregator如果您使用 Backbone.Marionette.application,就像在链接的示例中一样。

// in your view
...
success: function() {
app.vent.trigger('myview:modelsaved');
}
...

// in your layout initialize()
...
app.vent.on('myview:modelsaved', function(){
console.log('model saved in itemView');
});
...

如果您不使用 Backbone.Marionette.Application,您始终可以创建自己的 Backbone.Wreqr.EventAggregator。

关于events - Marionette 布局 : trigger event on child view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19220783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com