带有 slim.php 的 Backbone.js - 当我添加一个新模型时显示错误“Uncaught TypeError: Object [object Object] has no method 'create'”。请帮我将新模型添加到数据库中。
如果我使用 this.model.save() 方法,它会显示 500 Internal Sever Error。用户 create() 和 save() 方法的位置。我不清楚这一点。
谢谢。
// Models
window.Users = Backbone.Model.extend({
urlRoot:"./bb-api/users",
defaults:{
"name":"",
"email":"",
"designation":""
}
});
window.UsersCollection = Backbone.Collection.extend({
model:Users,
url:"./bb-api/users"
});
// Views
window.AddUserView = Backbone.View.extend({
template:_.template($('#new-user-tpl').html()),
initialize:function(){
this.model.bind("click", this.render, this);
},
render:function(){
$(this.el).html(this.template());
return this;
},
events:{
"click .add":"saveUser"
},
saveUser:function(){ //alert('saveUser');
this.model.set({
name:$("#name").val(),
email:$("#email").val(),
designation:$("#designation").val()
});
if(this.model.isNew()){
this.model.create(this.model);
}
}
});
// Router
var AppRouter = Backbone.Router.extend({
routes:{
"":"welcome",
"add":"addUser"
},
welcome:function(){
$('#content').html('Welcome to Backbone.js App');
},
addUser:function(){
this.addUserModel = new Users();
this.addUserView = new AddUserView({model:this.addUserModel});
$('#content').html(this.addUserView.render().el);
}
});
var app = new AppRouter();
Backbone.history.start();