访问流星火焰模板中的外部脚本

时间:2016-11-22 15:52:57

标签: javascript jquery meteor meteor-blaze

我在client / templates / 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中访问此外部脚本?

1 个答案:

答案 0 :(得分:0)

您在控制台上看到的内容确认QB.createSession存在。但请注意,createSession来电是对另一个 createSession的调用!

也就是说,我认为你会发现this.auth中的Qb.createSessionundefined的对象,而createSession不可用属于auth QB(未定义),而非QB.init(已定义)。

如果您在致电QB.createSession之前未运行init,则会发生这种情况。 the QuickBlox JavaScript SDK docs here中解释了add_filter('gform_confirmation', 'conditional_confirmation', 10, 4); function conditional_confirmation($confirmation, $refurl, $entry, $ajax) { if ($refurl == 'http://www.example.com') { $confirmation = array('redirect' => 'http://www.google.com'); } return $confirmation; }