Backbone.js的model.save返回类型错误? [英] Backbone.js model.save returns TypeError?
问题描述
我想实现Backbone.js的模型,看起来像这样在Django:
I'm trying to implement backbone.js model which looks like this in Django:
class Booking(models.Model):
date = models.DateField('date booked')
time = models.TimeField('time booked')
duration = models.PositiveSmallIntegerField('duration booked') #x * 15
user = models.ForeignKey(User, related_name='bookings')
room = models.ForeignKey(Room, related_name='bookings')
description = models.TextField()
/ API /预订:
它是经由TastyPie一个RESTful接口服务。
我的骨干模型建立这样按照说明这里:
It's served through a RESTful interface via TastyPie: /api/booking. My backbone model is set up like this as per instructions here:
window.Booking = Backbone.Model.extend({
url : function(){
return this.get('resource_uri') || this.collection.url;
},
urlRoot : "/api/booking",
defaults: {
user : "/api/user/5" //defaults to nobodys
}
});
window.Bookings = Backbone.Collection.extend({
url : BOOKING_API,
parse : function(data){
return data.objects;
}
});
现在想测试一下控制台是这样的:
Now trying to test it out in console like this:
var booking = new Booking({date : "2011-08-17", time: "12:45", duration: 30, room: "/api/room/1", description: "quickbook"});
booking.save();
以上code返回一个类型错误。我不明白我在做什么错了。
The above code returns a TypeError. I don't see what I'm doing wrong.
推荐答案
您没有定义resource_uri或将预订一个收藏。你所看到的类型错误是this.collection.url。你会看到相同的错误做booking.collection.url。像下面的东西应该做的伎俩。
You are not defining a resource_uri or putting your booking in a collection. The TypeError you are seeing is from this.collection.url. You would see the same error doing booking.collection.url. Something like the following should do the trick.
var bookings = new Bookings;
bookings.add(booking);
booking.save();
这篇关于Backbone.js的model.save返回类型错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!