Backbonejs模型没有在fetch()上找到正确的url,因此不会发送任何参数

时间:2013-09-10 20:12:12

标签: node.js rest backbone.js model

我在客户端有这个:

var Article = Backbone.Model.extend({
    urlRoot: '/wiki'
});
var ArticleView = Backbone.View.extend({
   render: function(){
     var article = new Article({term:'test'});
     article.fetch(); //sends a request to wiki, not to wiki/:term as i want to
   }
});

,这在服务器(节点)上:

//Never gets hit
app.get('/wiki/:term', function(req, res){
    console.log('/wiki/term');
});

app.get('/wiki', function(req, res){
    console.log('/wiki');
    console.log(req.params.term); //produces 'undefined'
});

我需要做什么才能从正确的路由(即wiki /:term)中获取骨干,并实际将term参数发送到服务器?

编辑: 我能够使用req.query.term检索服务器端的请求参数,而不是req.params.term

1 个答案:

答案 0 :(得分:1)

var Article = Backbone.Model.extend({
    urlRoot: '/wiki',
    url: function () {
      return this.urlRoot + "/" + encodeURIComponent(this.get("term"));
    }
});