2

このスクリプトは私には機能しません。

問題が.on('value')関数にあることは知っていますが、それが何であるかはわかりません。

これは私のスクリプトです:

$(document).ready(function(){

//dynamic content switching inside the content section
function writeContent(page){
    //setting post to default error message
    var post = "Sorry, This page is undergoing maintenance.";
    //firebase data reference for the clicked link's content
    var dataRef = new Firebase("https://cityartist.firebaseio.com/"+page);
    //inserting the available content to the 'post' variable 
    dataRef.on("value",function(snapshot){
        if(snapshot.val() != null){
            post = snapshot.val();
            alert("post="+post);
        }   
    });
    //showing content in the content section and changing the page's title
    $("#content").append("<article class='post'><p>"+post+"</p></article>");
    $("title").text("CityArtist.org - "+page);
}

//switching pages using the navigation bar
$(".menu_link").click(function(){
    writeContent($(this).attr("name"));
});
 });
4

2 に答える 2

2

Firebase はデータの到着を待たなければならないため、Firebase のコールバックは非同期で起動されることがよくあります。したがって、 on('value') コールバック コードは、「//showing content in ...」コードの /after/ と呼ばれています。代わりにこれを試すことができます:

dataRef.on("value",function(snapshot){
    if(snapshot.val() != null){
        post = snapshot.val();
        alert("post="+post);
    }
    //showing content in the content section.
    $("#content").append("<article class='post'><p>"+post+"</p></article>");
});
于 2013-07-22T15:46:49.313 に答える