3

I would like to change the url to end with .json in Backbone. Here's my issue:

I want to use the same URI for the HTML version of my site and the JSON API (used by the HTML version). So both my URIs should look like

GET website.com/users/1 to get info about the user with id 1 in HTML. The HTML page fetches website.com/users/1.json.

Howewer, backbone doesn't let me change the extension of the url of my models:

var User = Backbone.Model.extend({
  url:'users.json'
});

does'nt work

I understand that Backbone doesn't integrate the idea of extension in its URL, so what possibilitys do I have ?

4

1 回答 1

3

您可以覆盖 url 函数,并在该函数中生成所需的 url:

url: function(){
   return 'users/'+this.id+'.json'
}

骨干文档

于 2013-08-06T10:36:49.883 回答