简体   繁体   English

JavaScript自定义事件触发问题

[英]JavaScript custom event trigger issue

I have a details view which contains 2 buttons for Create/Edit. 我有一个详细信息视图,其中包含2个用于创建/编辑的按钮。

On click of these buttons, I display a modal popup 单击这些按钮后,我将显示一个模式弹出窗口

var modal = new MyAppModalView({
    model: person
});

modal.bind("afterSave", function(values) {
    self.showMessage('Sucessfully submitted for saving', false, true);
    return true;
});

Also within the Modal view code, I have: 同样在Modal视图代码中,我有:

if (isNew) {
//Create flow
    this.model.save(myJSON, {
        url: "myapp/persons/create",
        success: function() {
            self.trigger("afterSave");
            self.removeModal();
        }
    });
} else {
//Edit flow
    this.model.save(myJSON, {
        url: "myapp/persons/edit",
        success: function() {
            self.trigger("afterSave");
            self.removeModal();
        }
    });
}

Now after save for both Create/Edit, I trigger an event for afterSave 现在,在保存完创建/编辑后,我触发了afterSave事件

While the event is triggered for the Create flow, it does not get triggered for the Edit flow. 虽然为创建流触发了事件,但没有为编辑流触发事件。 I am not sure why does that happen? 我不确定为什么会这样?

I tried binding 2 separate evens for the modal (afterSave/afterUpdate) and triggering each for Create/Edit; 我尝试为模式(afterSave / afterUpdate)绑定2个单独的even,并为Create / Edit触发每一个; still the trigger does not get called for Edit (afterUpdate) 仍然不会触发触发器进行编辑(更新后)


The code is called within saveAction as below; 该代码在saveAction中调用,如下所示;

var MyAppModalView = BaseModal.extend({
....
saveAction: function(event) {
            var self = this;

}
});

You should not have to have a different URL to POST (create) and PUT (save) too. 您也不必为POST(创建)和PUT(保存)使用其他URL。 That would trim your code down to : 这会将您的代码缩减为:

this.model.save(myJSON, {
    success: function() {
        self.trigger("afterSave");
        self.removeModal();
    }
});

If removeModal() is being called then "afterSave" is being triggered but your edit view is causing the afterSave message to not be shown somehow. 如果正在调用removeModal(),则将触发“ afterSave”,但是您的编辑视图导致afterSave消息无法以某种方式显示。 You could replace your success message with an alert to verify that is the case. 您可以将成功消息替换为警报以验证情况是否如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM