所以我有mongodb的与会者集合
_id
event_id
name
profile_picture
user_id
我正在路由两个GET与会者集合
app.get('/attendee', event.eventAttendee);
app.get('/attendee/:event_id', event.findAttendeeByEventId);
...
...
exports.findAttendeeByEventId = function(req, res) {
var id = req.params.event_id;
console.log('Retrieving attendee: ' + id);
db.collection('attendees', function(err, collection) {
collection.find({'event_id': new BSON.ObjectID(id)}, function(err, item) {
item.toArray(function(err, items){
res.send(items);
});
});
});
};
exports.eventAttendee = function(req, res) {
res.send(200);
};
localhost:3000 \ attendee \ 528b4f85dafbffbc12000009给我这样的东西
[
{
"_id": "528b8c15e639d70e484b65ac",
"event_id": "528b4f85dafbffbc12000009",
"name": "John Doe",
"profile_picture": "https://graph.facebook.com/1205633679/picture",
"user_id": "5289e8bbdc91fe1803000009"
},
{
"_id": "528b8c58e639d70e484b65ad",
"event_id": "528b4f85dafbffbc12000009",
"name": "Mary Doe",
"picture": "https://graph.facebook.com/100000484671087/picture",
"user_id": "528a0b4e4833678c11000009"
}
]
我为带有集合
的与会者创建了一个Backbone Model类define([
'backbone',
'bootstrap',
'app/helpers/config'
], function(Backbone, config) {
var Attendee = Backbone.Model.extend({
urlRoot: "/attendee",
idAttribute: "event_id"
});
var AttendeeCollection = Backbone.Collection.extend({
model: Attendee,
url: "/attendee"
});
return {
model: Attendee,
collection: AttendeeCollection
};
});
然后一个ListView类列出所有参加者。所以这里有两个View,一个是列表视图,另一个是List View Item。
define([
'jquery',
'underscore',
'backbone',
'backbone.listview',
], function($,
_,
Backbone,
ListView)
{
var ListView = Backbone.ListView.extend({});
var EventAttendeeListItemView = Backbone.View.extend({
tagName: "div",
className: "dish col-md-4",
initialize: function () {
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
},
render: function () {
this.$el.html('Name: ' + this.model.get('name'));
return this;
}
});
return{
ListView: ListView,
ListItemView: EventAttendeeListItemView
};
});
我在其他文件中将此函数重新发送,其中EventAttendees指向上面的视图文件
var attendee = new Attendee.collection({
event_id: id
});
var listView = new EventAttendees.ListView({
className: 'list',
collection: attendee,
itemView: EventAttendees.ListItemView
});
attendee.fetch({
success: function(){
console.log("listView.render().el");
console.log(listView.render().el);
},
error: function(){
console.log("arguments");
console.log(arguments);
}
});
问题是集合没有获取成功,因此下面的行没有执行,我收到错误 - http://pastebin.com/HDaQaWcW(can有人也帮助如何找到错误的一些感觉?)
console.log("listView.render().el");
console.log(listView.render().el);
编辑: 根据@homtg,我得到了一个新的更新。
我收到错误,因为没有json 似乎我的backbonejs没有做localhost:3000 \ attendee \ 528b4f85dafbffbc12000009插入一个localhost:3000 \ attendee。 如何通过event_id告诉集合提取?
答案 0 :(得分:0)
idAttributemodel.idAttribute
模型的唯一标识符存储在id属性下。如果您直接与使用不同唯一键的后端(CouchDB,MongoDB)进行通信,则可以将Model的idAttribute设置为从该键透明地映射到id。
var Meal = Backbone.Model.extend({
idAttribute: "_id"
});
var cake = new Meal({ _id: 1, name: "Cake" });
alert("Cake id: " + cake.id);