2013-05-24 3 views
0

onclick 이벤트가 발생하면 Firebase 데이터베이스에서 데이터를 읽을 수 있기를 원합니다. 문서를 읽는 것에서부터 그럴 경우, once() 함수가 여기에 오는 방법 인 것 같습니다. 아이를 추가하는 것과 같은 특정 Firebase 이벤트에 데이터 읽기를 연결하려고 시도하지 않기 때문에, 누군가가 버튼을 클릭 할 때가 있습니다. 그 페이지. 여기에 내가 once() 부르는 줄은 ....once() 함수를 사용하여 Firebase 데이터베이스에서 데이터를 읽을 수 없습니다.

curPoll.once('value', function(curPollSnapshot) { 
    numElections = curPollSnapshot.val().NumElections; 
}); 

문제는 내가 curPoll는 데이터 참조로 설정된 경우에도, 데이터를 읽을 수 얻을 수 있다는 것입니다 및 numElections는 글로벌 변수입니다 내 스크립트. numElections는 정의되지 않은 상태로 되돌아옵니다. 사실, 나는 심지어 구문 오류가있는 것처럼 FireBug의 기능으로 들어가는 것조차 보이지 않습니다. 구문 오류가 있으면 알아낼 수 없으며 어쨌든 방화범이 끌린 경우 전체 스크립트가 전혀로드되지 않을 것이라고 생각합니다. 그러나 그것은 내가 무엇 때문에 어떤 일이 벌어지고 있는지를보기 위해 기능에 들어갈 수는 없다. 는 elections 참조 기준 curPoll의 자식으로 여기에 밀려시피 여기

function createPoll() 
{ 
    var pollName = $('#txtCreatePoll').val(); 
    if (pollName == "" || pollName == null) 
    { 
     alert("You must enter a name for your poll!"); 
     return; 
    } 
    else 
    { 
     pollsRef = new Firebase('https://poll-database.firebaseio.com/Polls'); 
     //TODO add error callback 
     curPoll = pollsRef.push({Name: pollName, Elections: null, NumElections: 0 }); 
     elections = curPoll.push(); 
     $('div#divCreateElections1').slideDown('slow'); 
    } 
} 

... curPoll 설정되는 기능이다. 나는 시도하고 선거에서 데이터를 참조 읽을 경우 여기

  function addElection() 
      { 
       curPoll.once('value', function(curPollSnapshot) { 
        numElections = curPollSnapshot.val().NumElections; 
       }); 
       var electionName = $('div#divCreateElections' + (parseInt(numElections)+1) + ' > input#txtCreateElection').val(); 
       var numberToElect = $('div#divCreateElections' + (parseInt(numElections)+1) + ' > input#txtNumElect').val(); 
       if (electionName == "" || electionName == null) 
       { 
        alert("You must enter a name for your election!"); 
        return; 
       } 
      } 

내가 말했듯이

는 numElections은 정의되지 않은오고 계속 .... 기능입니다. 여기 내 중포 기지 데이터베이스의 기본 구조 ...

Poll-Database > Polls > Poll1 
         Poll2 
         ... 
         Polln > Name 
           > NumElections 
           > Elections > Election1 
              > Election2 
              ... 
              > Electionn 

입니다 그리고 단지의 경우 여기 내 페이지의 본문 ...

<body> 
    <div id="divCreatePoll"> 
     Enter Name of New Poll: 
     <input type="text" id="txtCreatePoll" value="" /> 
     <br /><br /> 
     <button id="btnCreatePoll" onclick="createPoll(); return false;">CREATE POLL</button> 
     <p id="pError"></p> 
    </div> 
    <div id="divCreateElections1"> 
     Enter Election Name: 
     <input type="text" id="txtCreateElection" value="" /> 
     <br /><br /> 
     Enter Number of Candidates to Elect: 
     <input type="text" id="txtNumElect" value="" /> 
     <br /><br /> 
     <button id="btnCreateElection" onclick="addElection(); return false;">CREATE ELECTION</button> 
     <br /><br /> 
     <p id="pError"></p> 
    </div> 
</body> 

답변

2

문제는 once가 비동기 호출 점이다입니다. 따라서 즉시 numElections을 설정하지 않을 것입니다. 따라서 numElections를 사용할 때 아직 설정되지 않았습니다.

나머지 코드는 콜백 내부로 옮기면됩니다.

function addElection() { 
    curPoll.once('value', function(curPollSnapshot) { 
     numElections = curPollSnapshot.val().NumElections; 
     var electionName = $('div#divCreateElections' + (parseInt(numElections)+1) + ' > input#txtCreateElection').val(); 
     var numberToElect = $('div#divCreateElections' + (parseInt(numElections)+1) + ' > input#txtNumElect').val(); 
     if (electionName == "" || electionName == null) 
     { 
     alert("You must enter a name for your election!"); 
     return; 
     } 
    }); 
} 
+0

그래, 몇 분 전에 알아 냈어. 또 다른 질문은, programmaticallyly 추가 된 목록에 대한 참조를 명명하는 방법이 있습니까? var elections = curPoll.push(); ... 아니면 ref를 사용하고 Firebase이 생성 한 무작위로 할당 된 이름을 받아 들여야합니까? 감사. – MassStrike

+0

push() 대신에 child()를 사용해야합니다. – MassStrike

+0

push()를 사용하면'ref'가 반환되므로 push(). name()은 새 자식의 키를 가져옵니다. . – Kato

관련 문제