作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用来自 http://brian.io/lawnchair/ 的 Lawnchair JS在我的 Phonegap 应用程序中管理我的数据库记录。我是文档存储的新手,但对传统关系数据库很有经验。如果我想在另一条记录上引用一条记录,我该怎么办。例如:我有多个食品成分记录,我有多个食谱记录,在食谱 JSON 中,有没有办法引用成分记录?
最佳答案
要使用像 lawnchair 这样的 json 文档存储实现您想要的功能,您只需将引用文档的键名存储在文档中。
例如,您将拥有那些食品成分文件:
{key:'fi_1', name: 'rice', id: 1}
{key:'fi_2', name: 'corn', id: 2}
和那些食谱文件:
{key:'r_332', ingredients: ['fi_1', 'fi_54'], name: 'risotto', id: 332}
{key:'r_333', ingredient: ['fi_12'], name:'cookie', id: 333}
您可以将所有食谱的列表存储在一个文档中:
{key:'cookbook', recipes: ['r_1', 'r_2', .... 'r_332', 'r_333', .... ] }
然后您可以检索食谱文档:
store.get('cookbook', function(data) {
var recipes = data.recipes;
// do something with the list of all recipes
});
并寻找一个食谱来检索它的成分:
store.get('r_332', function(data) {
var ingredients = data.ingredients;
for (ingredient_key in ingredients) {
store.get(ingredient_key, function(ingredient_data) {
var name = ingredient_data.name;
var id = ingredient_data.id;
// do something with each ingredient
alert('ingredient: ' + name + ' - id: ' + id);
}
}
});
在上面的列表中,您可以只存储它们的 ID,而不是存储引用文档的完整键名,因为您应该知道它们的类型以及如何从中重新创建键名(食品成分:'fi_'前缀后跟 id,配方:'r_' 后跟 id..)。
关于javascript - 如何将记录与 lawnchair js 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10964578/
我是一名优秀的程序员,十分优秀!