This is a migrated thread and some comments may be shown as answers.

Click event of view model being called on page load

1 Answer 51 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 04 Dec 2013, 03:34 PM
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;
            }
        })
    }
});

1 Answer, 1 is accepted

Sort by
0
Thomas
Top achievements
Rank 1
answered on 05 Dec 2013, 01:35 PM
The issue was due to using an <input type=submit>.  Changing this to a <button> fixed the problem.
Tags
MVVM
Asked by
Thomas
Top achievements
Rank 1
Answers by
Thomas
Top achievements
Rank 1
Share this question
or