【问题标题】:How can you get a url for every piece of data?如何获取每条数据的 url?
【发布时间】:2013-08-02 23:07:29
【问题描述】:

我正在构建一个基于 Firebase 的 IM 平台,我希望每个用户都有一个地址,可以将他们引导至聊天室。

http://chatt3r.sitecloud.cytanium.com/

【问题讨论】:

  • 每当您创建新用户并将其与 ID 或短(小)URL 相关联时,只需创建一条记录。然后让您的页面从 URL 中读取 ID。这能回答你的问题吗?
  • 没有注册或渴望系统,您只需转到页面并生成一个url。
  • 您也可以在页面加载时创建插入语句。

标签: firebase


【解决方案1】:

<head>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://cdn.firebase.com/v0/firebase.js"></script>

    <script>
        var hash; // global incase needed elsewhere

        $(function() {
            hash = window.location.hash.replace("#", "");;
            myRootRef = new Firebase("http://xxxx.firebaseio.com");

            var postingRef;

            if (hash) {
                // user has been given a hashed URL from their friend, so sets firebase root to that place
                console.log(hash);

                postingRef = new Firebase("http://xxxx.firebaseio.com/chatrooms/" + hash);
            } else {
                // there is no hash, so the user is looking to share a URL for a new room
                console.log("no hash");

                postingRef = new Firebase("http://xxxx.firebaseio.com/chatrooms/");
                // push for a unique ID for the chatroom
                postingRef = postingRef.push();
                // exploit this unique ID to provide a unique ID host for you app
                window.location.hash = postingRef.toString().slice(34);
            }

            // listener
            // will pull all old messages up once bound
            postingRef.on("child_added", function(data) {
                console.log(data.val().user + ": " + data.val().message);
            });
            // later:
            postingRef.push({"message": "etc", "user": "Jimmybobjimbobjimbobjim"});
        });
    </script>

</head>

这在本地对我有用。您需要将 xxxx 更改为您所在的任何 URL,并在 .slice() 位添加第一部分的许多字符。

【讨论】:

  • 感谢您的帮助。
  • 没问题,有什么问题请告诉我:)
【解决方案2】:

哈希。

如果我正确理解您的问题,您希望能够共享一个 URL,该 URL 将允许单击该 URL 的任何人登录到同一个聊天室。

我为我曾经制作的一个 Firebase 应用程序做了这个。您需要做的第一件事是使用.push()method。将房间推送到 Firebase,然后使用toString() 方法获取推送的 URL。一些快速的 JS 字符串操作 - window.location.hash = pushRef.toString().slice(x) - 其中 'x' 是您想要剪切 URL 的任何位置。 window.location.hash 将为您设置哈希值。将哈希添加到共享 URL,然后进行下一步:

当你打开页面时你会想要一个哈希监听器来检查是否已经有一个哈希,所以$(window).bind('hashchange', function() {UpdateHash()}) 进入一个 doc.ready 函数,然后...

function UpdateHash() {
    global_windowHash = window.hash.replace("#", "");
    // to assign the hash to a global hash variable
    if (global_windowHash) {
        // if there was already a hash
        chatRef = new Firebase("[Your Firebase URL here]" + "/chatrooms/" + global_windowHash);
        // chatRef is the global that you append the chat data to, and listen from.
    } else {
        // there wasn't a hash, so you can auto-create a new one here, in which case:
        chatRef = new Firebase("[Your Firebase URL here]" + "/chatrooms/");
        chatRef = chatRef.push();
        window.location.hash = chatRef.toString().slice(x);
    }
}

我希望这会有所帮助(并且有效:P)。如果有任何问题或问题,那就问吧!

【讨论】:

  • 能否给我发一个 HTML 示例。
  • 我稍后再发一篇——我现在没有时间,抱歉——如果你想让我直接发送,你可以给我发电子邮件:)
  • 这就像使用 FireBase 在客户端之间同步数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-20
  • 1970-01-01
  • 2021-06-17
  • 2020-04-26
相关资源
最近更新 更多