Backbone.js集合未按id成功获取
Backbone.js collection not fetching successful by id
所以我有与会者的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''aattee''528b4ff85dafbfc12000009给了我这样的
[
{
"_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"
}
]
我用集合为与会者创建了一个骨干模型类
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类以列出所有与会者。所以这里有两个视图,一个是列表视图,另一个是"列表视图项"。
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(有人能帮助如何找到错误吗?)
console.log("listView.render().el");
console.log(listView.render().el);
编辑:根据@homtg,我得到了一个新的更新。
我收到一个错误,因为没有json我的backbonejs似乎没有在做localhost:3000''attedee''528b4f85dafbc12000009插入操作localhost:3000''Attedee。如何告诉集合按event_id获取
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);
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- difference between '$(<%= DDL.ID %>) & $('
- 使用Javascript获取所选选项ID
- 根据id将json数组组合为一个json数组
- 尽管链接成功并已成功下载,但未找到NPM模块
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 对id以某个字符串开头的元素进行计数
- 如果元素's的ID以数字开头
- 在另一个函数成功结束后调用该函数
- 在ajax成功后,cluetip不适用于首次点击活动元素
- 通过id和class属性获取元素
- 使 ajax 请求和成功数据显示在 id <选择>选择>部分中
- 将动态ID传递到成功主干上的url
- Backbone.js集合未按id成功获取
- Ajax - 在“成功”之后转到另一个页面,从那里的URL中检索用户ID
- 在没有元素ID的情况下切换Ajax成功后的数据值
- 如何获取成功模型的id.save()
- 如何将CSS类应用于刚刚成功的ID
- 从backbone.js集合获取model.id使用Tastypie创建成功调用
- AJAX成功后无法设置数据id