Click event of view model being called on page load

2 posts, 0 answers
  1. Thomas
    Thomas avatar
    5 posts
    Member since:
    Nov 2013

    Posted 04 Dec 2013 Link to this post

    I have this view, as below, which is bound the viewModel/module shown below (loaded up via requirejs as per the KendoUI Mobile Music Store).  When I load the page, the authenticate is being called (i.e., it's making that ajax network call as well).
    <div data-role="view" id="login-view" data-layout="blank"
         data-model="app.views.login.viewModel">
     
        <h1 style="text-align: center;">Title</h1>
        <form id="login">
            <input data-bind="value: username" id="username" type="text" placeholder="Username" /><br />
            <input data-bind="value: password" id="password" type="password" placeholder="Password" /><br />
            <input type="submit" value="Login" data-bind="click: authenticate" />
        </form>
    </div>
    define(["jQuery", "kendo", "modernizr", "app/environment"], function ($, kendo, modernizr, environment) {
        var authenticate = function (username, password) {
            return $.ajax({
                url: environment.apiConnection + '/canlogin?userid=' + username + '&password=' + password,
                type: 'get',
                dataType: 'jsonp'
            });
        }
     
        authenticate().done(function (data) {
            alert(data);
        }).fail(function() {
            alert('fail');
        })
     
        return {
            viewModel: kendo.observable({
                username: null,
                password: null,
                authenticate: function () {
                    var username = this.get('username'),
                        password = this.get('password');
     
                    authenticate(username, password);
     
                    if (!Modernizr.localstorage) {
                        alert('There was an error accessing LocalStorage on this device.');
                        return;
                    }
     
                    localStorage["username"] = username;
                    localStorage["password"] = password;
                }
            })
        }
    });
  2. Thomas
    Thomas avatar
    5 posts
    Member since:
    Nov 2013

    Posted 05 Dec 2013 Link to this post

    The issue was due to using an <input type=submit>.  Changing this to a <button> fixed the problem.
  3. Kendo UI is VS 2017 Ready
Back to Top