要使用像草坪椅这样的 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,recipe : 'r_' 后跟 id..)。