Backbone嵌套集合url示例

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

标签: javascript backbone.js

我一直在阅读this article,嵌套收集部分设置网址如下:

'donut_shops /'+ this.id +'/ donuts'

我想知道这将如何映射到web api控制器和动作等,即donut_shops / 5 / donuts会在控制器上调用什么?我可以理解,如果id在最后,那么它会调用在id中传递的甜甜圈动作,但我感到困惑的是id在中间。

var DonutShop = Backbone.Model.extend({
  defaults : {
  name : "Untitled"
},

initialize : function() {
// When you extend a Backbone.Model and give it an initialize function,
// the function is called when you instantiate an instance of your Model.

// The initialize function is used repeatedly in Backbone's prototypes. We'll be seeing this again
this.donuts = new Donuts;
this.donuts.url = 'donut_shops/' + this.id + "/donuts";
  }
});

谢谢, 加里

3 个答案:

答案 0 :(得分:0)

以这种方式嵌套可访问基础资源的嵌入式集合。因此,在这种情况下,有许多donut_shops,每个都有很多donuts。所以使用此URL:

'/donut_shops/5/donuts'

GET donuts donut_shop id为{{1}}的所有{{1}}。

答案 1 :(得分:0)

donuts_shops是商店的集合

donuts_shops/5是ID为

的商店

donuts_shops/5/donuts是一个集合,如果店里的甜甜圈有id 5

donuts_shops/donuts所有商店中所有甜甜圈的集合

donuts_shops/5/donuts/6是ID为5的商店中ID为6的甜甜圈

答案 2 :(得分:0)

好的,我想我已经在stackoverflow上发布的这个问题中找到了我的问题的答案:

nested resources in ASP.net MVC 4 WebAPI

我认为一半的技能是知道你在寻找什么: - )

相关问题