0

我有一个包含许多网址的 API,例如:

/users/profile -> 获取当前连接的用户
 /users/bestfriend -> 获取当前连接用户的最好朋友(最好朋友的最好朋友)
 /users/wife -> 获取当前连接用户的妻子


 /users/friends -> 获取当前连接用户的好友
 /users/bestfriends -> 获取当前连接用户的好友
 /users/childrens -> 获取当前连接用户的孩子
 /users/neighbours -> 获取当前连接用户的邻居

我是 Backbone 的新手,我真的不知道当前连接的用户是否应该作为一个集合或一个简单的模型来获取,例如。

我知道当前用户可能会像 Backbone 文档提到的那样被提升,但这不是重点,只是假设我不能提升当前用户模型。


那么如何将我的获取重定向到 API URL?我应该每次都提供类似{"operation":"profile"}or的选项{"operation":"friends"},还是在获取之前修改集合 url?

谢谢

4

3 回答 3

1

Use the model.urlRoot property to specify the URL of the model if you wish to use the model 'outside' of a collection. It'll automatically append an id to the end of the URL if it's a new model (i.e. id==null) and send a POST on creation. Else it'll fetch (GET) at the specified url.

If you don't want to have an id appended at the end: Just overwrite model.url to generate the actual endpoint URL that you want to fetch/put/post to.

Collections also have a property called url - if the url or urlRoot of the model isn't specified the collection's url is picked up as the root (if that model is part of that collection).

So you could just do either of these:

model.urlRoot = '/users/profile'; //appends id on put/delete requests
model.url = '/users/profile'; //direct endpoint. Can also be a function
collection.url = '/users/friends'; //fetch endpoint for collection. Could also be used by models to put to with appended id.

The Backbone's doc is quite comprehensive and self explanatory. I suggest you read these parts for example Model.urlRoot

UPDATE: As per your new URLs

I think the confusion is because of the way URLs are designed. It's not good practice to have end-points as plural/singular:

Ex: /user/friends - fine get all friends of user. But which user? Better still: /users/{id}/friends - get friends of a specific user.

GET specific friend for user: /users/{id}/friends/{id}

I suggest adding ids to your URLs to make the most of backbone's built in REST-able functionality. Else, just define your own properties and logic and have separate function like 'save' to fire $.ajax calls yourself and do something on success/error callbacks.

Whether they are models or collections will totally be up to you. Fortunately/unfortunately there is more than one way to do it in Backbone. If you follow BB's way it'll offer a cleaner way of doing things since the framework expects and follows certain conventions. If not, you can always custom create. I've done both ways. Custom creating seems easier, following convention takes some planning but keeps it much cleaner albeit hides/abstracts away stuff from the programmer 'under the hood' whereas explicit ajax calls make it explicit.

It's a tradeoff. I suggest restructure your endpoints and use the url properties accordingly. Else just do what makes sense to you and your team and stick with it.

于 2012-08-13T17:57:57.327 回答
1

您必须创建多个模型和集合,其中任何一个都与您的 URL 相关。

var Profile = Backbone.Model.extend({
  url: "/users/profile"
});

var BestFriend = Backbone.Model.extend({
  url: "/users/bestfriend"
});

// ...

var Friend = Backbone.Model.extend({});

var Friends = Backbone.Collection.extend({
  model: Friend,
  url: "/users/friends"
});

您也可以Friend用作以下的父类BestFriend

var BestFriend = Friend.extend({
  url: "/users/bestfriend"
});

甚至为每个Person创建一个类作为父类:

var Person = Backbone.Model.extend({});

var Friends = Backbone.Collection.extend({
  model: Person,
  url: "/users/friends"
});

var Profile = Person.extend({
  url: "/users/profile"
});

var BestFriend = Person.extend({
  url: "/users/bestfriend"
});

由于您有单独的 URL 来检索每个元素的数据,因此您应该进行几次提取:

var profile = new Profile();
var bestFriend = new BestFriend();
var friends = new Friends();

profile.fetch();
bestfriend.fetch();
friends.fetch();

您可以使用以下方式将所有元素引用到您的Profile实例中:

var Profile = Person.extend({
  url: "/users/profile",
  initialize: function( opts ){
    this.bestFriend = opt.bestfriend;
    // ...
  }
});

var bestFriend = new BestFriend();
var friends = new Friends();

var profile = new Profile({
  bestfriend: bestfriend
});

有几种方法可以组织元素,也可以收听关于它们的事件,但这只是一个品味问题,以及您更喜欢哪种架构。

于 2012-08-14T10:03:02.180 回答
1

基本上我同意第一个答案,但为了简单起见,您可以开始为用户使用 id 并单独获取它们。这可能会产生一些额外的负载,但它应该保持代码更干净,直到您需要优化。

您可能会遇到当前 API 设计的问题。如果将来你想要妻子最好的朋友怎么办?您将拥有一个具有 URL“/user/wife”的对象,然后您会说“/user/wife/bestfriend”。接下来有人想扩展它并拥有“/user/wife/bestfriend/children/”。这在后端解析会相当复杂。

本质上,不是单独调用妻子和孩子,而是让 profile 调用返回关联用户的 id:

/profile
{
  id: 165735,
  name: "Peter",
  age: 45,
  wifeId: 246247,
  childrenIds: [352356, 134098]
}

然后取妻子将只是:

/users/246247

这将使妻子成为真正的用户,而不仅仅是某人的妻子。检索用户时,您可以在此处检查安全性,因为无论如何您都将关系存储在数据库中。

于 2012-08-27T16:20:53.700 回答