【发布时间】:2017-04-06 09:18:39
【问题描述】:
我的客户端/模板/main.html 中有这个:
<head>
<title>app boil</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<script src="https://cdnjs.cloudflare.com/ajax/libs/quickblox/2.4.0/quickblox.min.js"></script>
</head>
所以我调用的是 quickblox api。这提供了一个 QB 对象。
我现在有包含以下代码的 client/templates/quickblox/quickbloxcall.js:
import { Template } from 'meteor/templating';
import './quickbloxcall.html'
Template.quickbloxcall.onRendered(function () {
console.log(QB.createSession);
});
Template.quickbloxcall.events({
'submit .quickblox-form'(event) {
var user = {
id: 4448514,
name: 'chatuserweb1',
login: 'chatuserweb1',
pass: 'chatuserweb1'
};
QB.createSession({login: user.login, password: user.pass}, function(err, res) {
if (res) {
QB.chat.connect({userId: user.id, password: user.pass}, function(err, roster) {
if (err) {
console.log(err);
} else {
/*
* (Object) roster - The user contact list
* roster = {
* '1126541': {subscription: 'both', ask: null}, // you and user with ID 1126541 subscribed to each other.
* '1126542': {subscription: 'none', ask: null}, // you don't have subscription but user maybe has
* '1126543': {subscription: 'none', ask: 'subscribe'}, // you haven't had subscription earlier but now you asked for it
* };
*/
}
});
}else{
console.log(err);
}
});
},
});
在上面的代码中,当我提交表单时,我在控制台中收到此错误:
Uncaught TypeError: Cannot read property 'createSession' of undefined(…)
所以这意味着在 Template.quickblox.events 提交事件处理程序中无法访问 QB 对象。
但是在 console.log() 中我得到了这个:
function (params, callback) {
this.auth.createSession(params, callback);
}
所以这意味着 Template.quickbloxcall.onRendered 正在正确加载 QB 对象。
如何在 Template.quickblox.events 中访问这个外部脚本?
【问题讨论】:
-
你试过$.getScript吗?
-
为什么不使用 NPM 版本的 Quickblox?也许它可以解决你的问题
标签: javascript jquery meteor meteor-blaze