简体   繁体   English

如何获取Backbone.js发出不同的请求?

[英]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. 我正在尝试通过用Backbone.js + jQuery编写的javascript应用程序来实现文件上传/下载。 The server is set up as a REST server (as needed by Backbone) to accept get requests as specified below. 该服务器被设置为REST服务器(Backbone需要),以接受如下所述的get请求。

Example HTTP gets are: HTTP获得的示例是:

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”的模型。 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. 如果我理解正确,则需要根据模型上设置的当前属性来调用其他URL。

You can do this by overriding the url() function on the Item model. 您可以通过覆盖Item模型上的url()函数来执行此操作。 So you might end up with something like this on your Item model: 因此,您可能会在Item模型上得到如下所示的结果:

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

Scott 史考特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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