访问 Backbone 获取参数

Access Backbone fetch parameters

在 Backbone 集合上调用 fetch 时,我需要能够向服务器传递一些信息。在我的前端,我有这段代码,将 data:{} 连同成功和错误回调一起传递给 fetch 函数。查看 Backbone 的文档 fetch:

http://backbonejs.org/#Collection-fetch

也许我没有将参数正确传递给 fetch 函数?也许它们应该在数组 []...

var lineupCollection = new LineupCollection([]);
var loadTeamsOnPageLoad = (function() {

    lineupCollection.url = '/api/lineups';
    lineupCollection.fetch(
        {
            processData: true,
            data: {
            team_id:$("#team_id").attr("value")
        }
        },
        {
            success: function (result) {
                if(window.teamDashboardTableView === undefined){
                    window.teamDashboardTableView = new TeamDashboardTableView({el: $("#team-dashboard-table-div")});
                }
                window.teamDashboardTableView.render();
            },
            error: function (err) {
                setTimeout(function () {
                    alert('error fetching lineupCollection - ' + err.message);
                }, 1);

            }
        }
    );
})();

这会将 Backbone 集合加载到 table。

后台调用这个方法:

exports.getBackboneLineups = function(req,res,next){

    var user_id = req.mainUser._id;
    console.log('req.params',req.params); //empty {}
    console.log('req.team',req.team); //undefined
    console.log('team_id',req.team_id); //undefined
    console.log('req.data',req.data); //undefined
    console.log('req.body',req.body); //empty {}
    var team_id = req.team._id;  //undefined EXCEPTION, can't read property _id of undefined
    var system_db = req.system_db;
    var Lineup = LineupModel.getNewLineup(system_db,user_id);

    Lineup.find({team_id:team_id}, function (err, lineups) {
        if (err) {
            return next(err);
        }
        res.json(lineups);
    });

};

但是,就如何访问我应该从客户端传递到服务器的数据而言,我完全失败了。我真的找不到一个很好的例子,而且我已经厌倦了猜测。执行此操作的最佳方法是什么?

编辑: 还尝试在同一个 JavaScript 对象中传递 {data:{}, success: function(), error: function()},但这没有用。

fetch 只接受一个参数(参见 http://backbonejs.org/#Collection-fetch)。相反,您可能应该更改后端以在资源的 URL 中指定团队 ID。例如:

lineupCollection.url = function(){ return '/api/lineups?team_id=' + this.team_id; };
lineupCollection.team_id = $("#team_id").attr("value");
lineupCollection.fetch({ success: ..., error: ...});