使用一个查询而不是多个查询使用Backbone引导数据 [英] Bootstrapping data with Backbone using one query as opposed to multiple queries

查看:56
本文介绍了使用一个查询而不是多个查询使用Backbone引导数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我错了,请纠正我,但它是一个复杂的查询,比多个小查询便宜吗->

Please correct me if I am wrong, but is one complex query less expensive than multiple small queries -> Question? Seems the question listed has opposing views. I was always taught that the most expensive operation is the opening and closing of the connection to the db. If that is the case, what is the most efficient way to bootstrap your data on application startup?

为简单起见,可以说我的应用程序需要最新帖子,最新类别,已登录用户和最新标签的集合.现在,我可以在一个查询中获取所有这些数据,也可以单独获取这些数据.您会推荐哪一个?如果我只进行单个查询,通常如何在Backbone中执行此操作,以及如何通过Restful API进行此操作?

For simplicities sake, lets say my application requires a collection of latest posts, latest categories, the logged in user, and latest tags. Now I could fetch all this data in one query, or I could fetch this data individually. Which one would you recommend? If I went with a single query, how do you usually do this in Backbone, and how would you do this over a Restful API?

非常感谢

泰隆

推荐答案

backbone-relational(您提到的)在主干内部提供了一个解决方案,而没有使用在相关问题中建议的jQuery.getJSON.只需返回所有内容以响应根查询,然后定义与JSON结构匹配的关系即可.

backbone-relational (which you mention) provides a solution inside backbone without using jQuery.getJSON as suggested in the related question. Simply return everything in a response to the root query and define relations matching the JSON structure.

例如,我刚刚写了一些东西,它在响应的根目录中返回相关配置文件对象的数组,然后在其他地方使用profile_ids.只需确保同时定义顶级关系和引用(使用keySource和keyDestination)即可.

For example, I've just written something which returns an array of relevant profile objects in the root of the response, and then uses profile_ids elsewhere. Just be sure to define both the top-level relation and the references (using keySource and keyDestination).

这篇关于使用一个查询而不是多个查询使用Backbone引导数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆