如何在Backbone.js中使用MongoDB ReferenceField?

时间:2012-05-31 18:05:40

标签: python mongodb backbone.js flask mongoengine

我在Flask中定义了两个Mongo文档:

class Category(db.Document):
    name = db.StringField(max_length=255, required=True)
    created = db.DateTimeField(default=datetime.datetime.now, required=False)

    def __unicode__(self):
        return self.name

class Event(db.Document):
    description = db.StringField(max_length=255, required=True)
    created = db.DateTimeField(default=datetime.datetime.now, required=False)
    categories = db.ListField(db.ReferenceField(Category),required=False)
    meta = {
        'indexes': ['-description','created'],
        'ordering': ['-created']
    }

我使用一个小的REST API来为我的Backbone模型提供服务。

window.EventModel = Backbone.Model.extend({
    defaults: {},
    initialize: function Event(){},
    validate: function(a){},
    url: function(){
        return this.id ? '/events/' + this.id : '/events';
    }

})

window.CategoryModel = Backbone.Model.extend({
    defaults: {},
    initialize: function Expense(){},
    validate: function(a){},
    url: function(){
        [...]
    }

})

这是我的JSON回复:

[{
        "description": "MyEvent",
        "created": {"$date": 1338404831615},
        "id": "4fc653ffbe51030887000004",
        "categories": [{
                "$ref": "category", 
                "$id": {"$oid": "4fc652efbe51030887000001"}
        }, {
                "$ref": "category",
                "$id": {"$oid": "4fc65304be51030887000002"}
        }]
}]

我想知道如何将事件模型与类别模型链接到Backbone?

注意:在这种情况下,Backbone-relationnal无法正常工作。

有什么想法吗?非常感谢!

0 个答案:

没有答案