【发布时间】:2014-06-09 20:20:41
【问题描述】:
我正在尝试让一个简单的应用程序正常工作。它使用主干、苗条的框架和科尔多瓦。现在,我要做的就是从 MySQL 数据库中获取用户模型。客户端代码如下:
// Cordova is ready
function onDeviceReady()
{
// This little code snippet prepends our root to any Ajax call.
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.url = 'http://localhost/~nmeibergen/IBMI/www/' + options.url;
});
var profileView = new ProfileView();
var Router = Backbone.Router.extend({
routes: {
"": "home"
}
});
var router = new Router();
router.on('route:home', function() {
profileView.render();
});
Backbone.history.start();
}
var Users = Backbone.Collection.extend({
url: 'index.php/users',
model: User
});
var User = Backbone.Model.extend({
urlRoot: 'index.php/users',
defaults: {
user_id: 1,
name: 'Nathan',
}
});
var ProfileView = Backbone.View.extend({
el: '#home',
render: function () {
var users = new Users();
alert('all fine');
users.fetch();
}
});
只要我只是去http://localhost/~nmeibergen/IBMI/www/index.php/users,我就会得到预期的 JSON 对象:
[{"user_id":"1","name":"Jhonathan "},{"user_id":"0","name":"Nathan"}]
但是,当运行上述代码时,它会出现“一切正常”的警报,但随后会出现以下错误和堆栈跟踪:
TypeError: 'undefined' is not an object (evaluating 'targetModel.prototype') in backbone.js:1:689
set backbone.js:1:689
success backbone.js:1:686
fire jquery-1.11.1.js:3119
fireWith jquery-1.11.1.js:3231
done jquery-1.11.1.js:9275
callback jquery-1.11.1.js:9685
我似乎无法找出代码有什么问题,它应该是非常基本的。感谢您的关注!
【问题讨论】:
-
@muistooshort:我刚刚编辑了我的问题,包括非缩小主干中的堆栈跟踪:)
标签: backbone.js cordova slim