简体   繁体   中英

How do I get Backbone.js to issue different requests?

I am trying to implement file upload/download via a javascript application written with Backbone.js + jQuery. The server is set up as a REST server (as needed by Backbone) to accept get requests as specified below.

Example HTTP gets are:

GET /     #This means get all the files in the root directory
GET /foo/ #This means get all the files in the foo firectory
GET /bar  #This means get the bar file

I want the client application to lazy load the directory information (essentially on request). In the backbone framework I have set up a model called 'Item'. Item contains a field called type which specifies what type of item it is. If the type is 'folder' then I want to be able to list files in that folder. How do I get backbone to output different gets depending on the information in the Model?

If I understand you correctly you need to call a different URL based on the the current attributes set on a model.

You can do this by overriding the url() function on the Item model. So you might end up with something like this on your Item model:

url: function() {
  switch(this.get('type')) {
  case: 'foo':
    return '/foo';
    break;
  case: 'bar':
    return '/bar';
    break;
  default:
    return '/';
  }
}

Scott

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM