我想获取firebase中的项目列表,但该项目的每个元素都有一个相关项目列表。我无法使用firebase-util或firebase数组$extend功能获取列表。
我的firebase数据如下所示:
items
item1
name: "Item 1"
user: user1
images
image1: true
image2: true
item2
name: "Item 2"
user: user1
images:
image3: true
image4: true
item3
name: "Item 3"
user: user2
images:
image5: true
image6: true
users
user1
name: "User 1"
email: "user1@email.com"
user2
name: "User 2"
email: "user2@email.com"
images
image1
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image2
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image3
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image4
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image5
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
我只想获得包含所有数据的项目列表。类似的东西:
items
item1
name: "Item 1"
user
name: "User 1"
email: "user1@email.com"
images
image1
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image2
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
item2
name: "Item 2"
user
name: "User 1"
email: "user1@email.com"
images
image3
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image4
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
item3
name: "Item 3"
user
name: "User 2"
email: "user2@email.com"
images
image5
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image6
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
它看起来像是一个相当常见的用例,但我被困在这里。我尝试了this解决方案(两种方式),但我无法解决问题。数据结构也有点不同,因为我需要关联另一个列表中的列表。
答案 0 :(得分:6)
目标是显示项目列表。
每个项目都有一个图像列表。
最初,显示项目列表和每个项目的一个图像。
建议的方法:
要填充项目列表及其初始缩略图,我们需要一个单独的节点来从中提取初始设置。
更新了项目节点
items:
item_id_xx: //this should be a Firebase generated node name
name: "Item 2"
user: "uid_for_user_1"
images:
image3: "data:image/jpeg;base64,/9j/..."
image4: "data:image/jpeg;base64,/9j/..."
这是用于主列表的节点,用户可以点击项目缩略图以获取更多详细信息:
item_list_for_ui
random_node_0
item_id: "item_id_aa"
name: "Item 1" //if you want to display the name in the list
initial_thumb: "data:image/jpeg;base64,/9j/..." //initial thumb
link_to: "image1"
random_node_1
item_id: "item_id_xx"
name: "Item 2"
initial_thumb: "data:image/jpeg;base64,/9j/..."
link_to: "image3"
random_node_2
item_id: "item_id_qq"
name: "Item 3"
initial_thumb: "data:image/jpeg;base64,/9j/..."
link_to: "image1"
当应用程序启动时,从items_list_for_ui节点填充列表。
节点很浅,包含Firebase item_id,项目名称(如果需要),获取初始图像缩略图的链接以及Firebase中主图像的link_to。
例如:如果用户单击项目2的缩略图,则可以使用带有.value的observeSingleEvent加载项目详细信息
/ items / item_id_xx / images / image3
你可以通过添加say,翻译链接到item_list_for_ui
来解释这个问题。 random_node_1
item_id: "item_id_xx"
name: "Item 2"
initial_thumb: "data:image/jpeg;base64,/9j/..."
thumb_link: "image3"
rollover_thumb: "external link to rollover"
rollover_link: "image4"
这种结构非常灵活,您可以通过更新相应的子节点来更改要在主列表中显示的缩略图和翻转。
它也很有效,因为它避免加载数百个项目和数百个子图像节点 - 加载所有这些节点和子节点会使ui过载(在某些情况下)。
使用这种结构,item_list_for_ui是紧凑的,所以即使有数百个项目,它也是该数据的一小部分。
你可能会对自己说'自己,这是重复的数据'。是的,它是,并且Firebase中的重复数据是一个正常的过程,并鼓励:它使结构更平坦,更快地进行查询和处理数据。
答案 1 :(得分:2)
如果您拥有所有可用数据,则可以遍历图像并将其元数据用作数据库其余部分的密钥。
var itemsArr = [];
for(var i in items) {
var item = items[i];
var images = [];
for(var image in item[images]) {
item.push(images[image]);
}
itemsArr.push({
name: item.name,
user: users[item.user],
images: images
});
}
哪一个应该产生一个像这样的对象数组:
{
name: "Item 1",
user: {
name: "User 1",
email: "user1@email.com"
},
images: [{
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
},{
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
}]
}
答案 2 :(得分:2)
感谢@Jay和@Eric的答案,他们非常乐于助人,我的解决方案有两点兼顾。我会解释我是怎么想出来的。
首先,我更改了架构,并为项目的主要图片添加了一个新密钥。我称之为cover
。但回答原始问题,我会加载所有图像。所以这是新的items
架构:
items
item1
name: "Item 1"
user: user1
cover: image1
images
image1: true
image2: true
item2
name: "Item 2"
user: user1
cover: image3
images:
image3: true
image4: true
item3
name: "Item 3"
user: user2
cover: image5
images:
image5: true
image6: true
然后,这就是我如何获得上面提到的列表(使用async库)。这可能是一种更好的方法来实现同样的目标:
getItems: function(cb){
var items = ref.child("items");
items.on("value", function(snapshot){
var item_length = snapshot.numChildren(),
final_items = [],
readed = 0;
ref.child("items").on("child_added", function(item){
var item_id = item.key(),
itemData = item.val(),
user = ref.child("users").child(itemData.user),
cover = ref.child("images").child(itemData.cover),
images = new Firebase.util.NormalizedCollection(
[ref.child("items").child(item_id).child("images"),'alertImages'],
ref.child('images')
).select('images.image','images.thumb').ref();
async.parallel([
function(callback){
user.on("value", function(user_snap){
callback(null, user_snap.val());
});
},
function(callback){
images.on("value", function(images_snap){
callback(null, images_snap.val());
});
},
function(callback){
cover.on("value", function(cover_snap){
callback(null, cover_snap.val());
});
}
], function(err, results){
if(!!err){
cb(err,null)
}else{
itemData.user = results[0];
itemData.images = results[1];
itemData.cover = results[2];
final_items.push(itemData);
readed += 1;
if(readed === item_length){
cb(null,final_items);
}
}
});
});
});
}
这将输出如下内容:
item1
name: "Item 1"
cover:
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
user
name: "User 1"
email: "user1@email.com"
images
image1
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image2
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
item2
name: "Item 2"
cover:
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
user
name: "User 1"
email: "user1@email.com"
images
image3
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image4
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
item3
name: "Item 3"
cover:
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
user
name: "User 2"
email: "user2@email.com"
images
image5
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."
image6
image: "data:image/jpeg;base64,/9j/..."
thumb: "data:image/jpeg;base64,/9j/..."