【发布时间】:2013-09-30 04:14:18
【问题描述】:
我正在使用 firebase 的 foreach 从这个 url 获取树中的每个孩子
目标,当页面加载时从firebase中抓取一个随机项目并显示它
数据结构
grabbit (table name)
active (for active items for sale)
category (the category of the item ie womensClothes, MensShoes etc)
unique id of the item
页面加载进入http://gamerholic.firebase.com/grabbit/active 并抓取任何一个类别并将其返回..
脚本
var grabbitRef = new Firebase('https://gamerholic.firebaseIO.com/grabbit/active/');
grabbitRef.on('value', function(snapshot) {
if(snapshot.val() === null) {
alert("invalid");
} else {
// get snap shot data:
snapshot.forEach(function(snapshot) {
var name = snapshot.name();
alert(name);
});
}
});
在我有一个随机类别“电子产品”后,我会得到一个新快照并让它返回电子产品中的任何随机项目
var grabbitRef = new Firebase('https://gamerholic.firebaseIO.com/grabbit/active/'+name);
grabbitRef.on('value', function(snapshot) {
if(snapshot.val() === null) {
alert("invalid");
} else {
// get snap shot data:
snapshot.forEach(function(snapshot) {
var id = snapshot.name();
alert(id);
});
}
});
我现在可以通过 id 获取项目的详细信息
var grabbitRef = new Firebase('https://gamerholic.firebaseIO.com/grabbit/active/'+name+'/'+id);
【问题讨论】:
-
避免在 Firebase 中嵌套数据:firebase.google.com/docs/database/web/…
标签: javascript jquery firebase