[英]How to show a user profile in a model from the list of users fetched using fetch api with javascript
[英]How to fetch on /users/profile and /users/friends with BackboneJS
我有很多网址的API,例如:
/users/profile -> GET the currently connected user /users/bestfriend -> GET the currently connected users' best friend (most bestfriend of bestfriends) /users/wife -> GET the currently connected users' wife /users/friends -> GET the currently connected users' friends /users/bestfriends -> GET the currently connected users' best friends /users/childrens -> GET the currently connected users' childrens /users/neighbours -> GET the currently connected users' neighbours
我是Backbone的新手,我真的不知道是否应该将当前连接的用户作为集合或简单模型来获取。
我知道当前用户可能会像Backbone doc提到的那样受到提振,但这不是重点,只是假设我无法提升当前用户模型。
那么,如何将提取的内容重定向到API URL? 我应该每次提供诸如{"operation":"profile"}
或{"operation":"friends"}
,还是在执行提取之前修改集合网址?
谢谢
如果要使用集合的“外部”模型,请使用model.urlRoot
属性指定模型的URL。 如果是新模型,它将自动在URL末尾附加一个id(即id==null
),并在创建时发送POST。 否则,它将在指定的网址获取(GET)。
如果不想在末尾附加ID:只需覆盖model.url
即可生成要获取/放入/发布到的实际端点URL。
集合还具有一个称为url
的属性-如果未指定模型的url或urlRoot,则将集合的url作为根(如果该模型是该集合的一部分)。
因此,您可以执行以下任一操作:
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.
骨干网的文档非常全面,可以自我解释。 我建议您阅读这些部分,例如Model.urlRoot
更新 :根据您的新URL
我认为造成混淆的原因是URL的设计方式。 端点为复数/单数不是一个好习惯:
例如: /user/friends
很好地获得user的所有朋友。 但是哪个用户呢? 更妙的是: /users/{id}/friends
获取特定用户的朋友。
为用户获取特定的朋友: /users/{id}/friends/{id}
我建议在您的URL中添加ID,以充分利用骨干网内置的REST功能。 否则,只需定义您自己的属性和逻辑并具有单独的功能(如“保存”)即可触发$.ajax
调用您自己并在成功/错误回调上执行某些操作。
无论是模型还是收藏,完全取决于您。 幸运的是,在Backbone中有多种方法可以做到这一点。 如果您遵循BB的方法,它将提供一种更简洁的处理方式,因为该框架期望并遵循某些约定。 如果没有,您可以随时自定义创建。 我做过两种方式。 自定义创建似乎更容易,遵循约定需要一些计划,但是可以使其更加整洁,尽管“隐藏”隐藏/抽象了程序员的内容,而显式ajax调用使其变得显式。
这是一个权衡。 我建议您重组端点并相应地使用url属性。 否则,请尽一切可能对您和您的团队有意义,并坚持下去。
您必须创建几个与您的一个URLS相关的模型和集合。
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
});
有几种方法可以组织元素,也可以听取有关事件的信息,但这只是品味的问题,您更喜欢哪种架构。
从本质上讲,我同意第一个答案,但为简单起见,您可以开始为用户使用id并分别获取它们。 这可能会产生一些额外的负载,但是在您需要进行优化之前,它应该使代码更整洁。
当前的API设计可能会遇到问题。 如果将来您想要妻子的最好的朋友怎么办? 您将拥有一个URL为“ / user / wife”的对象,然后说“ / user / wife / bestfriend”。 接下来,某人想要扩展它并使用“ /用户/妻子/最好的朋友/孩子/”。 在后端解析非常复杂。
本质上,让配置文件调用返回关联用户的ID而不是分别呼叫妻子和孩子:
/profile
{
id: 165735,
name: "Peter",
age: 45,
wifeId: 246247,
childrenIds: [352356, 134098]
}
然后拿到妻子将是:
/users/246247
这将使妻子成为真正的用户,而不仅仅是某人的妻子。 检索用户时,您可以在此处检查安全性,因为无论如何您都已将关系存储在数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.