需要单击“提交”按钮两次以在IBM Mobile First中调用onSuccess Adapter功能

时间:2016-05-26 09:59:34

标签: angularjs ibm-mobilefirst

我在IBM MobileFirst Platform Foundation应用程序中使用AngularJS。我有一个登录表单,当我单击登录按钮时,它会收集一些数据并将其传递给适配器调用函数。如果成功,它永远不会调用成功loginSuccess函数。我需要单击登录按钮两次,以便调用成功函数。

为什么我需要点击两次按钮?

app.js

var app = angular.module('myApp', ['ui.router']);

app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/login');
    $stateProvider.state('login', {
        url: '/login',
        templateUrl: 'view/login.html',
        controller: 'loginController'
    });
});

login.js

app.controller('loginController', function($scope) {

    $scope.login = function(usrName, pass) {

        var usrName = angular.element('#usrName').val();

        var pass = angular.element('#pass').val();
        console.log("Variable values: " + usrName + " : " + pass);
        $scope.loginProcedure = {
            adapter: 'SQL',
            procedure: 'login',
            parameters: [usrName, pass]
        };

        WL.Client.invokeProcedure($scope.loginProcedure, {
            onSuccess: $scope.loginSuccess,
            onFailure: $scope.loginFailure
        });

        $scope.loginSuccess = function(data) {
            $scope.data = data;
            $scope.resultSet = data.resultSet;
            console.log('success: ' + JSON.stringify($scope.data + " : " + $scope.resultSet));
        };

        $scope.loginFailure = function() {
            console.log('failed');
        };
    }
});

SQL-impl.js

/*
 *  Licensed Materials - Property of IBM
 *  5725-I43 (C) Copyright IBM Corp. 2011, 2013. All Rights Reserved.
 *  US Government Users Restricted Rights - Use, duplication or
 *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */
/************************************************************************
 * Implementation code for procedure - 'procedure1'
 *
 *
 * @return - invocationResult
 */
var loginStatement = WL.Server.createSQLStatement("select usrname,password from jay1 where usrname = ? and password=? ");

function login(usr, pass) {
    return WL.Server.invokeSQLStatement({
        preparedStatement: loginStatement,
        parameters: [usr, pass]
    });
}

/************************************************************************
 * Implementation code for procedure - 'procedure2'
 *
 *
 * @return - invocationResult
 */

function procedure2(param) {
    return WL.Server.invokeSQLStoredProcedure({
        procedure: "storedProcedure2",
        parameters: [param]
    });
}

的index.html

<!DOCTYPE HTML>
<html>

<head>
    <meta charset="UTF-8">
    <title>AB</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <!--
                    <link rel="shortcut icon" href="images/favicon.png">
                    <link rel="apple-touch-icon" href="images/apple-touch-icon.png"> 
                -->
    <link rel="stylesheet" href="css/main.css" />
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/bootstrap-responsive.min.css" />

    <script>
        window.$ = window.jQuery = WLJQ;
    </script>
</head>

<body ng-app="myApp">
    <!--application UI goes here-->

    <div id="view" ui-view></div>

    <script src="js/initOptions.js"></script>
    <script src="js/main.js"></script>
    <script src="js/messages.js"></script>
    <script src="library/jquery.min.js"></script>
    <script src="library/angular.1.4.9.js"></script>
    <script src="library/angular-ui-router.min.js"></script>
    <script src="library/bootstrap.min.js"></script>
    <script src="controller/app.js"></script>
    <script src="controller/login.js"></script>
</body>

</html>

的login.html

<div ng-controller="loginController" class="col-xs-12 col-sm-6 col-md-4 center-block" id="lgBlock">

    <!-- Login Box Start -->

    <div class="panel panel-primary">
        <div class="panel-heading">Login</div>
        <div class="panel-body">
            <form class="form-group" name="lgForm">
                <input type="text" class="form-control" id="usrName" required />
                <input type="password" class="form-control" id="pass" required />
                <input type="submit" class="btn btn-primary btn-block" id="submit" value="Login" ng-click="login()" />
            </form>
        </div>
    </div>

    <!-- Login Box End -->

    <!--  error Modal start -->

    <div class="modal" role="modal" id="errorPopup">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">Error</div>
                <div class="modal-body"></div>
            </div>
        </div>
    </div>

    <!--  error Modal End -->

</div>

使用angularJS $scope解析JSON数据的正确方法是什么?

1)$scope.data = data; $scope.resultSet = data.resultSet;

2)$scope.data = data; $scope.resultSet = $scope.data.resultSet;

1 个答案:

答案 0 :(得分:2)

您应该利用AngularJS双向绑定更新您的登录控制器,如下所示:

<强> login.js

app.controller('loginController', function($scope) {

    $scope.login = function() {

        // TODO: if block to validate user input i.e., username and password

        $scope.loginProcedure = {
            adapter: 'SQL',
            procedure: 'login',
            parameters: [$scope.username, $scope.password]
        };

        WL.Client.invokeProcedure($scope.loginProcedure, {
            onSuccess: $scope.loginSuccess,
            onFailure: $scope.loginFailure
        });

        console.log($scope.loginProcedure);

        $scope.loginSuccess = function(data) {
            $scope.data = data;
            // resultSet is part of data so you don't need to refrences
            // you can use $scope.data.resultSet to access resultSet
            console.log('success: ' + JSON.stringify($scope.data + " : " + $scope.data.resultSet));
        };

        $scope.loginFailure = function() {
            console.log('failed');
        };
    }
});

您还应该更新视图以声明usernamepassword

的绑定

<强>的login.html

<div ng-controller="loginController" class="col-xs-12 col-sm-6 col-md-4 center-block" id="lgBlock">

    <!-- Login Box Start -->

    <div class="panel panel-primary">
        <div class="panel-heading">Login</div>
        <div class="panel-body">
            <form class="form-group" name="lgForm">
                <input type="text" class="form-control" id="usrName" ng-model="username" required />
                <input type="password" class="form-control" id="pass" ng-model="password" required />
                <input type="submit" class="btn btn-primary btn-block" id="submit" value="Login" ng-click="login()" />
            </form>
        </div>
    </div>

    <!-- Login Box End -->

    <!--  error Modal start -->

    <div class="modal" role="modal" id="errorPopup">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">Error</div>
                <div class="modal-body"></div>
            </div>
        </div>
    </div>

    <!--  error Modal End -->

</div>

将数据传递到$scope的方式完全取决于您和您的需求。我的建议是确定您需要的内容并仅将其绑定到$scope,例如,如果您只需要data.resultSet,那么您应该$scope.resultSet。另一方面,如果您需要完全访问data对象,则应将data绑定到$scope,然后您仍然可以从那里访问resultSet,即$scope.data.resultSet

<强>建议:

仅将您需要在视图中引用的内容绑定到$scope,例如,您不需要访问视图中的loginSuccessloginFailure,并且您只使用这两个功能一次,所以我建议您按如下方式更新loginController

app.controller('loginController', function($scope) {

    $scope.login = function() {

        // TODO: if block to validate user input i.e., username and password

        $scope.loginProcedure = {
            adapter: 'SQL',
            procedure: 'login',
            parameters: [$scope.username, $scope.password]
        };

        WL.Client.invokeProcedure($scope.loginProcedure, {
            onSuccess: function(data) {
                $scope.data = data;
                // resultSet is part of data so you don't need to refrences
                // you can use $scope.data.resultSet to access resultSet
                console.log('success: ' + JSON.stringify($scope.data + " : " + $scope.data.resultSet));
            },
            onFailure: function() {
                console.log('failed');
            }
        });
    }
});