簡體   English   中英

使用jQuery觸發動態自定義事件

[英]Fire dynamic custom event with jQuery

我在Symfony2項目中正在研究Json / AJAX實現。 一切正常(模式,重定向等),但我的“火災事件”實施卻不起作用。

這是我的代碼:

$(document).trigger(data.content, [data.data]);

data.content包含事件的名稱,在這種情況下為“ lesson_editor.added_step”,而data.data包含HTML字符串。

我收到錯誤信息Uncaught TypeError: undefined is not a function jQuery庫中Uncaught TypeError: undefined is not a function 使用Chrome調試器,上面的觸發器就是導致它的那一行。

如果我將data.content替換為字符串"my.test"它將起作用。

感謝您的幫助。

完整的JSON字符串:

{"type":"event","content":"lesson_editor_added_step","data":"\n    <div class=\"panel\">\n        <div class=\"panel-heading\">\n            <span class=\"panel-title\">\n                <a data-toggle=\"collapse\" data-parent=\"#steps\" href=\"#step25\">\n                    asdfasdfasdf\n                <\/a>\n            <\/span>\n            <div class=\"pull-right\">\n                <a href=\"#\" class=\"btn btn-xs btn-success\" data-toggle=\"tooltip\" title=\"Add action\">\n                    <i class=\"fa fa-plus\"><\/i>\n                <\/a>\n                <a href=\"#\" class=\"btn btn-xs btn-warning\" data-toggle=\"tooltip\" title=\"Edit step\">\n                    <i class=\"fa fa-edit\"><\/i>\n                <\/a>\n                <a href=\"#\" class=\"btn btn-xs btn-danger\" data-toggle=\"tooltip\" title=\"Delete step\">\n                    <i class=\"fa fa-times\"><\/i>\n                <\/a>\n            <\/div>\n        <\/div>\n        <div id=\"step25\" class=\"panel-collapse collapse in\">\n            <div class=\"panel-body\" id=\"actions25\">\n                            <\/div>\n        <\/div>\n    <\/div>\n","flashBag":{"success":["Step added"]}}

AJAX電話

 the URL
 $.ajax({
     url: url,
     type: type,
     data: data,
     beforeSend: this.options.beforeSendCallback
 })
.done($.proxy(function(data) {
    if (data.type === undefined) {
        alert("Ajax data invalid");
        return false;
    }
    // Switch data type
    switch (data.type) {
        case 'modal':
            // Create modal
            $(document).bootstrapModal("createModal", data.content);
            break;
        case 'redirect':
            // Redirect to page
            window.location.replace(data.content);
            break;
        case 'event':
            // Fire a custom event
            $(document).trigger(data.content, [data.data]);
            break;
    }
    // Check if flashbag exists
    if (data.flashBag) {
        $.each(data.flashBag, function(type, messages) {
            $.each(messages, function(index, message) {
                new PNotify({
                    type: type,
                    text: message
                });
            });
        });
    }
    // Fire event
    $(document).trigger('ajax_done', [data.data]);
}), this)
.fail(this.options.failCallback);

最好通過以下方式觸發自定義事件:

$(document).trigger(data.content, data.data);

並使用.on事件處理程序捕獲它:

$(document).on(data.content, function(event, args...) {
   // do something beneficial for your career here
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM