【问题标题】:Access external script in meteor blaze Template.events在流星火焰 Template.events 中访问外部脚本
【发布时间】: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


【解决方案1】:

您在控制台上看到的确实证实了QB.createSession 的存在。但是请注意,在 createSession 调用中是对 另一个 createSession!

的调用

也就是说,我想你会发现Qb.createSession里面的this.authundefined的对象,而不可用的createSession属于auth(未定义),而不是QB(已定义) .

如果您在调用QB.createSession 之前没有运行QB.init,就会发生这种情况。 initthe QuickBlox JavaScript SDK docs here 中有一点解释。

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 2017-02-09
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多