gpt4 book ai didi

javascript - Marionette 做 self 渲染的最佳方式

转载 作者:行者123 更新时间:2023-11-29 18:22:14 26 4
gpt4 key购买 nike

你好,这是我的小代码:

我不知道如何让这个 Marionette 更...保存功能太像 Backbone ...

 self.model.save(null, {
success: function(){
self.render();
var vFormSuccess = new VFormSuccess();
this.$(".return").html(vFormSuccess.render().$el);
}
var VFormSuccess = Marionette.ItemView.extend({
template: "#form-success"

});

http://jsfiddle.net/Yazpj/724/

最佳答案

我会使用事件来显示您的成功 View ,并使用布局来显示您的成功 View (如果它进入不同的位置)。

MyLayout = Marionette.Layout.extend({
template: "#layout-template",
regions: {
form: ".form",
notification: ".return"
}
initialize: function () {
this.listenTo(this.model,'sync',this.showSuccess);
this.form.show(new FormView({model: this.model}));
},
showSuccess: function () {
this.notification.show(new VFormSuccess());
}
});

或者,您可以只对一个区域执行相同的操作,并让 FormView 成为布局本身。您只需要确保 layout-template 中存在与通知区域匹配的元素即可。

MyLayout = Marionette.Layout.extend({
template: "#layout-template",
regions: {
notification: ".return"
}
initialize: function () {
this.listenTo(this.model,'sync',this.showSuccess);
},
showSuccess: function () {
this.notification.show(new VFormSuccess());
}
});

这允许您做什么:

然后,如果需要,您可以很容易地显示错误 View 。您可以将 initialize 替换为

initialize: function () {
this.listenTo(this.model,'sync',this.showSuccess);
this.listenTo(this.model,'error',this.showError);
},

然后添加以下内容,确保您创建了一个 VFormError View 。

showError: function () {
this.notification.show(new VFormError());
}

关于javascript - Marionette 做 self 渲染的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17318019/

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