Durandal JS和Google Plus登录Javascript API [英] Durandal JS and Google Plus Sign in Javascript API

查看:98
本文介绍了Durandal JS和Google Plus登录Javascript API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试使用Durandal构建移动应用.但是,我在使用Google加上JS API集成时遇到了麻烦.我认为这是问题所在:gapi的javascript客户端附带了异步加载,因此在未定义gapi的情况下,durandal初始化将崩溃,因此,css组件也无法正确呈现.我已经尝试将以下脚本标记放在login.js视图的login.html文件中,甚至放在main.js的require.js部分中,但仍然无法正常工作.链接到Google Api的脚本:

So I'm trying to build a mobile app using Durandal. However, I am having trouble to do so with Google plus JS API integration. I think here is the problem: The javascript client for gapi comes with asynchronous loading, so durandal initialization will crash having gapi undefined and because of this, the css components also do not render properly also. I have tried putting the following script tags in the login.html file of my view for login.js or even in the require.js section of my main.js but it's still not working. Scripts for linking to Google Api:

<script src="https://apis.google.com/js/client:platform.js" async defer>    </script>
<script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>

//onSignInCallBack function to call in login.js after user signs in vis Google Plus
onSignInCallback: function(authResult) {
                    gapi.client.load('plus','v1').then(function() {

                        if (authResult['access_token']) {
                            $('#authOps').show('slow');
                            $('#gConnect').hide();
                            var user_access_token = authResult['access_token'];
                            var id_token = authResult['id_token'];
                        } else if (authResult['error']) {
                            // There was an error, which means the user is not signed in.
                            console.log('There was an error: ' + authResult['error']);
                            $('#authOps').hide('slow');
                            $('#gConnect').show();
                        }
                        console.log('authResult', authResult);
                    });
                },

这是我的main.js文件:

Here is my main.js file:

    requirejs.config({
    paths: {
        'text': '../Scripts/text',
        'durandal': '../Scripts/durandal',
        'plugins': '../Scripts/durandal/plugins',
        'transitions': '../Scripts/durandal/transitions',
        'knockout' : '../Scripts/knockout-3.1.0',
        'bootstrap': '../Scripts/bootstrap',
        'jquery':'../Scripts/jquery-1.9.1',
        'async':'../Scripts/async'
    }
});


define(['durandal/system',
        'durandal/app',
        'durandal/viewLocator',
        'durandal/binder',
        'utils/routines',
        'async!https://apis.google.com/js/platform.js?onload=onLoadCallback',
        'async!https://apis.google.com/js/client:platform.js'

], function (system, app, viewLocator, binder,routines,callback,gapi) {

    //>>excludeStart("build", true);
    system.debug(true);
    //>>excludeEnd("build");

    app.configurePlugins({
        router: true,
        dialog: true,
        widget: true
    });

    app.start().then(function() {
        //Replace 'viewmodels' in the moduleId with 'views' to locate the view.
        //Look for partial views in a 'views' folder in the root.
        viewLocator.useConvention();

        //Show the app by setting the root view model for our application with a transition.
        app.setRoot('shell/shell', 'entrance');

        // override bad route behavior to write to 
        // console log and show error toast
        /*
        router.handleInvalidRoute = function (route, params) {
            logger.logError('No route found', route, 'main', true);
        };
        */
    });
});

这是我的index.html:

Here is my index.html:

   <!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="apple-touch-startup-image" href="/Content/images/ios-startup-image-    landscape.png" media="(orientation:landscape)" />
    <link rel="apple-touch-startup-image" href="/Content/images/ios-startup-image-portrait.png" media="(orientation:portrait)" />
    <link rel="apple-touch-icon" href="~/Content/images/icon.png" />
    <link href="/Content/ie10mobile.css" rel="stylesheet" />
    <link href="/Content/bootstrap.min.css" rel="stylesheet" />
    <link href="/Content/font-awesome.min.css" rel="stylesheet" />
    <link href="/Content/durandal.css" rel="stylesheet" />
    <link href="/Content/starterkit.css" rel="stylesheet" />   

    <script type="text/javascript">

        if (navigator.userAgent.match(/IEMobile\/10\.0/)) {
            var msViewportStyle = document.createElement("style");
            var mq = "@@-ms-viewport{width:auto!important}";
            msViewportStyle.appendChild(document.createTextNode(mq));
            document.getElementsByTagName("head")[0].appendChild(msViewportStyle);
        }
    </script>
</head>
<body>
    <div id="applicationHost">
        <div class="text-center">
            <br/><br/><br/><br/>
            <i class="fa fa-spinner fa-spin"></i>
            <div class="message">
                Loading....
            </div>
        </div>
    </div>
    <script type="text/javascript" src="Scripts/require.js" data-main="/App/main"></script>

</body>
</html>

有人尝试过将Google plus登录与Durandal集成,并遇到同样的问题吗?帮助和建议深表感谢!

Have anyone ever tried to integrate google plus sign in with Durandal and have this same issue? Help and suggestion is much appreciated!!

推荐答案

对于在Durandal(或换句话说,在RequireJS)下的远程异步加载,您将需要async,goog和propertyParser

For remote async loads under Durandal (or, in other words, under RequireJS), you'll need the async, goog, and propertyParser plugins. See the readme at that link.

作为示例,这是我们为Google Maps做的事情:

As an example, here's what we do for Google Maps:

在RequireJS的paths属性中(通常在Durandal的main.js文件中),顺序如下:

'async': '../Scripts/plugins/async',
'propertyParser': '../Scripts/plugins/propertyParser',
'goog': '../Scripts/plugins/goog'

同样在Durandal的main.js文件中的定义,位于启动应用程序的主要功能之前:

define('jquery', function() { return jQuery; });
define('knockout', ko);
define('gmaps', ['async!http://maps.google.com/maps/api/js?sensor=false'],
    function () {            
        return window.google.maps;
    });

define(['durandal/system',
        'durandal/app',
        'durandal/viewLocator',
        'bindings',
        'extenders',
        'validations'],
    function (system, app, viewLocator, bindings, extenders, validations) {

在远程API URL之前注意async!.还要注意,我们实际上并未为应用程序的主要入口点定义gmap.我们在主入口点之前 设置了全局定义.

Notice the async! before the remote API URL. Also notice that we don't actually define gmaps for the main entry point of the application. We set up a global define before the main entry point.

请告诉我是否有帮助.

这篇关于Durandal JS和Google Plus登录Javascript API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆