0

I have this on the client:

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
   }
});

and this on the server (node):

//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'
});

What do i need to do to make backbone fetch from the correct route (i.e wiki/:term) and actually send the term parameter to the server?

EDIT: i was able to retrieve the request parameter on the server side using req.query.term, not req.params.term

4

1 回答 1

1
var Article = Backbone.Model.extend({
    urlRoot: '/wiki',
    url: function () {
      return this.urlRoot + "/" + encodeURIComponent(this.get("term"));
    }
});
于 2013-09-10T20:17:52.477 回答