beforeClose无法在jGrowl中运行?

时间:2010-04-09 23:30:47

标签: jquery cookies

我有以下代码从ASP.NET页面中提取json数据并将其显示为通知。该代码还会记录已经通过的内容并将其存储在一个数组中,以防止它在同一个会话中再次显示。

我现在正在尝试实现功能,以便当用户关闭消息时,它的ID被记录在cookie中以防止它再次显示。为此,我试图在beforeClose事件触发时写入cookie。

除了保存到cookie位之外,其他所有工作都很好。我错过了我的代码有什么问题吗?

var alreadyGrowled = new Array();
var noteCookie = $.cookie("notificationsViewed");
if (noteCookie != null) { alreadyGrowled = noteCookie.split(","); }

function growlCheckNew() {
$.getJSON('getNotifications.aspx', function(data) {
    $(data).each(function(entryIndex, entry) {
        var newMessage = true;
        $(alreadyGrowled).each(function(index, msg_id) {
            if (entry['ID'] == msg_id) {
                newMessage = false;
            }
        });

        if (newMessage == true) {
            $.jGrowl(entry['Message'], {
                sticky: true,
                header: entry['Title'],
                beforeClose: function(e, m) {
                    $.cookie("notificationsViewed", entry['ID']);
                }
            });
        }
        alreadyGrowled.push(entry['ID']);
    });
});

}

3 个答案:

答案 0 :(得分:0)

此:

        $.jGrowl(entry['Message'], {
            sticky: true,
            header: entry['Title'],
            beforeClose: function(e, m) {
                $.cookie("notificationsViewed", entry['ID']);
            }
        });

需要:

        var id = entry['ID'];
        $.jGrowl(entry['Message'], {
            sticky: true,
            header: entry['Title'],
            beforeClose: function(e, m) {
                $.cookie("notificationsViewed", id);
            }
        });

您的beforeClose稍后会发生,包含已创建元素的上下文,但此时entry不可用,您可能会看到Cookie设置为未定义或错误...这应该可以解决这个问题通过显式将id传递给闭包。

答案 1 :(得分:0)

行。使用close事件,我可以将消息的id添加到cookie中,我的下一个问题是在新会话中cookie似乎为空/空,尽管我设置了到期日。

//notification functions
var alreadyGrowled = new Array();
var keepClosed = new Array();
var noteCookie = $.cookie("notificationsViewed");
if (!noteCookie) {
    $.cookie("notificationsViewed", "", { expires: 365, path: '/' });
} else {
    keepClosed = noteCookie.split(",");
}

function growlCheckNew() {
     $.getJSON('getNotifications.aspx', function(data) {
    $(data).each(function(entryIndex, entry) {
        var newMessage = true;
        $(alreadyGrowled).each(function(index, msg_id) {
            if (entry['ID'] == msg_id) {
                newMessage = false;
            }
        });

        $(keepClosed).each(function(index, msg_id) {
            if (entry['ID'] == msg_id) {
                newMessage = false;
            }
        });

        if (newMessage == true) {
            var id = entry['ID'];
            alreadyGrowled.push(id);
            $.jGrowl(entry['Message'], {
                sticky: true,
                header: entry['Title'],
                close: function(e, m) {
                    keepClosed.push(id);
                    $.cookie("notificationsViewed", keepClosed);
                }
            });
        }
    });
});
}

答案 2 :(得分:0)

    $.jGrowl(entry['Message'], {
        myId:entry['ID'],
        sticky: true,
        header: entry['Title'],
        close: function(e, m, o) {
            $.cookie("notificationsViewed", o.myId);
        }
    });

通过选项传递您的Id参数。