簡體   English   中英

SwampDragon未定義

[英]SwampDragon is not defined

在這里嘗試教程

settings.py

    DRAGON_URL = 'http://localhost:9999/'

    TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'notifications.context_processors.dragon_url',
)

SwampDragon設置

SWAMP_DRAGON_CONNECTION = ('swampdragon.connections.sockjs_connection.DjangoSubscriberConnection', '/data')

context_processors.py

from django.conf import settings

def dragon_url(request):
    return {'DRAGON_URL': settings.DRAGON_URL}

home.html

{% load staticfiles %}
{% load swampdragon_tags %}

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>

<h1>Notifications demo</h1>

<!-- This is our list of notifications -->
<ul id="notifications">
{% for notification in object_list %}
<li>{{ notification.message }}</li>
{% endfor %}
</ul>


<!-- SwampDragon -->
{% swampdragon_settings %}
<script type="text/javascript" src="{% static 'swampdragon/js/dist/swampdragon.js' %}"></script>


<script type="text/javascript" src="{% static 'swampdragon/js/vendor/sockjs-0.3.4.min.js' %}"></script>

<script type="text/javascript" src="{% static 'swampdragon/js/legacy/swampdragon-vanilla.js' %}"></script>
<script type="text/javascript" src="{% static 'notifications.js' %}"></script>
<script type="text/javascript" src="{% static 'swampdragon/js/dist/datamapper.js' %}"></script>

<script type="text/javascript" src="{{ DRAGON_URL }}settings.js"></script>

<!-- notifications -->
<script type="text/javascript" src="{% static 'notifications.js' %}"></script>

</body>
</html>

控制台錯誤

ReferenceError: SwampDragon is not defined

var sdInstance = new SwampDragon(options);

還嘗試在控制台中顯示設置

<script>
console.log(window.swampdragon_settings);
</script>

我懂了

endpoint        "/data"

不知道怎么了。 請讓我知道是否需要更多信息。

如果需要點凍結

backports.ssl-match-hostname==3.4.0.2
certifi==2015.4.28
Django==1.7
django-filter==0.10.0
djangorestframework==3.1.3
Markdown==2.6.2
python-dateutil==2.4.2
redis==2.10.3
six==1.9.0
sockjs-tornado==1.0.1
SwampDragon==0.4.2.2
tornado==4.2
tornado-redis==2.4.18
wheel==0.24.0

似乎問題是我正在遵循的教程存在一些問題。 請參閱問題以獲取教程鏈接。

我將notifications.js更改為:

// Ask the browser for permission to show notifications
// Taken from https://developer.mozilla.org/en-US/docs/Web   /API/Notification/Using_Web_Notifications
window.addEventListener('load', function () {
    Notification.requestPermission(function (status) {
        // This allows to use Notification.permission with Chrome/Safari
        if (Notification.permission !== status) {
            Notification.permission = status;
        }
    });
});


// Create an instance of vanilla dragon
//var dragon = swampdragon.open({onopen: onOpen, onchannelmessage:    onChannelMessage});

// This is the list of notifications
var notificationsList = document.getElementById("notifications");

// New channel message received
swampdragon.onChannelMessage(function(channels, message){
// Add the notification
addNotification((message.data));
});

// SwampDragon connection open
swampdragon.open(function() {
// Once the connection is open, subscribe to notifications
swampdragon.subscribe('notifications', 'notifications');
});


// Add new notifications
function addNotification(notification) {
// If we have permission to show browser notifications
// we can show the notifiaction
if (window.Notification && Notification.permission === "granted") {
    new Notification(notification.message);
}

// Add the new notification
var li = document.createElement("li");
notificationsList.insertBefore(li, notificationsList.firstChild);
li.innerHTML = notification.message;

// Remove excess notifications
while (notificationsList.getElementsByTagName("li").length > 5) {
    notificationsList.getElementsByTagName("li")[5].remove();
}
}

而不是使用

var dragon = new VanillaDragon(...)

我改用

swampdragon.<function name> 

而且有效。

暫無
暫無

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

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