首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义SwampDragon

未定义SwampDragon
EN

Stack Overflow用户
提问于 2015-07-15 01:36:21
回答 1查看 619关注 0票数 0

试用教程here

settings.py

代码语言:javascript
复制
    DRAGON_URL = 'http://localhost:9999/'

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

SwampDragon设置

代码语言:javascript
复制
SWAMP_DRAGON_CONNECTION = ('swampdragon.connections.sockjs_connection.DjangoSubscriberConnection', '/data')

context_processors.py

代码语言:javascript
复制
from django.conf import settings

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

home.html

代码语言:javascript
复制
{% 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>

控制台中出现错误

代码语言:javascript
复制
ReferenceError: SwampDragon is not defined

var sdInstance = new SwampDragon(options);

我还尝试在控制台中显示设置

代码语言:javascript
复制
<script>
console.log(window.swampdragon_settings);
</script>

我得到了

代码语言:javascript
复制
endpoint        "/data"

不知道出了什么问题。如果需要更多信息,请告诉我。

如果需要,请冻结pip

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-07-15 23:15:47

似乎问题是我所遵循的教程有一些问题。有关教程的链接,请参阅问题。

我将notifications.js更改为:

代码语言:javascript
复制
// 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();
}
}

不是使用

代码语言:javascript
复制
var dragon = new VanillaDragon(...)

我转而使用

代码语言:javascript
复制
swampdragon.<function name> 

而且它是有效的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31413563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档