我有Uncaught TypeError:undefined不是var user = new this.login;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport"
content="width=device-width, user-scalable=no" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" href="css/jquery.mobile-1.4.2.min.css">
<script src="js/jquery.js"></script>
<script src="js/jquery.mobile-1.4.2.min.js"></script>
<script src="js/underscore.js"></script>
<script src="js/backbone.js"></script>
<title>Hello World</title>
</head>
<body>
<div id="deviceready" data-role="page">
<div data-role="header">Header</div>
<div role="main" class="ui-content">
<form action="javascript:void(0);" id="form-login">
<label for="username">User name:</label> <input name="username"
id="username" value="" type="text"> <label for="password">User
name:</label> <input name="password" id="password" value="" type="password">
<button id="submit-login"
class="ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-user" onsubmit="">Login</button>
</form>
</div>
<div data-role="footer">Footer</div>
</div>
<div id="formResults" data-role="page">
<div data-role="header">Header</div>
<div role="main" class="ui-content">Hello from success</div>
<div data-role="footer">Footer</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
app.submit('#form-login');
</script>
</body>
</html>
js文件:
var app = {
// Application Constructor
initialize : function() {
this.bindEvents();
},
bindEvents : function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady : function() {
navigator.splashscreen.hide();
},
submit : function(id) {
$(id).submit(function() {
var user = new this.login;
user.set('username', 'test');
user.set('password', 'test');
user.save({
success : function(model, response) {
$(':mobile-pagecontainer').pagecontainer('change',
'#formResults', {
reload : true
});
},
error : function(model, response) {
alert('login error');
}
});
});
},
Login : Backbone.Model.extend({
defaults : {
"username" : "test",
"password" : "test",
"token" : ""
},
url : "http://mobile.dev/login",
}),
};
答案 0 :(得分:1)
这里的问题是您的this
已更改,您可以将其分配给变量。
例如
submit : function(id) {
var self = this;
$(id).submit(function() {
var user = new self.Login;
//...
一般情况下,我建议你阅读一下JavaScript上下文。