javascript - Backbone.js 集合未按 id 成功获取

标签 javascript mongodb backbone.js backbone.js-collections backbone-model

所以我有与会者的 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 模型类

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,一个是 ListView ,另一个是 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 指向上述 View 文件

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 获取?

最佳答案

idAttributemodel.idAttribute 

模型的唯一标识符存储在 id 属性下。如果您直接与使用不同唯一键的后端(CouchDB、MongoDB)通信,您可以设置模型的 idAttribute 以透明地从该键映射到 id。

var Meal = Backbone.Model.extend({
  idAttribute: "_id"
});

var cake = new Meal({ _id: 1, name: "Cake" });
alert("Cake id: " + cake.id);

关于javascript - Backbone.js 集合未按 id 成功获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20263391/

相关文章:

javascript - 如何从 orgChart 中的框中删除标题

Windows 7 机器上的 MongoDB : No connection could be made

node.js - mongodb.connect不执行回调函数

javascript - Backbone - 将模型更改保存到本地对象

jquery - 保存整个集合的最佳实践?

javascript - Backbone.js 执行 OPTIONS 而不是 POST

javascript - 过滤器 vs map Reactjs 和 jsx

javascript - 将事件悬停在更高 z-index 覆盖的元素上?

javascript - 检查元素是否存在

java - 在java中的mongo聚合查询中使用 "hint"的语法