我知道在分离模型和模板时很容易处理模型。 但是当我在视图中附加模板并将模型传递给它时,我想触发一个事件来销毁我传递的模型。以下是代码的一部分:
ImageView.extends Marionette.ItemView
events:
'click [data-event=delete]' : 'delete'
ui:
'image' : 'div[name=image]'
template : """
<img src='<%=image.get('url')%>' />
<a data-event=delete>X</a>
"""
addImage : (image) ->
@ui.image.append _.template(@template)({ image: image })
delete : -> //I can trigger this event, but no idea how to pass the image model in order to
//delete it
upload.destory()
图像显示正确,这意味着模板接收到模型,但如何在触发事件中传递图像模型?
答案 0 :(得分:1)
您应该使用Marionette.ItemView's callback methods来帮助您销毁Image model
。
ItemView的onRender
回调将是正确的选择。
var ImageView = Marionette.ItemView.extend({
// you initialization, ui bindings, etc
onBeforeRender: function () {
// Triggered before an ItemView is rendered.
},
// Here you can destroy your model
onRender: function () {
// Triggered after the view has been rendered.
this.model.destroy();
}
});
呈现视图后将调用 onRender
,this.model.destroy()
将销毁与当前ItemView
关联的模型。