2013-09-21 4 views
0

문자열을 사용하고 컬렉션에서 문서를 찾음에 따라 세션을 설정하는 함수가 있습니다. 이 특별한 경우에는 문자열이 게임 name의 가능한 이름이고 세션을 해당 게임에 설정합니다. _idURL Meteor.startup 내에서 구문 분석이 작동하지 않습니다.

문제는 템플릿 이벤트에 바인딩 할 때 예상대로 작동하지만 Meteor.startup에 함수를 호출하면 전혀 작동하지 않습니다. 참고로 - 현재로서는 자동 게시를 실행 중입니다. (나는이 단계 이후에 발행/구독 설정을 조정할 계획.) 이것은 문자열을 받아 적절하게 세션을 설정하는 기능입니다

: 나는에이 기능을 사용하는 홈 페이지에서

var enterExisting = function(n) { 
    var g = Games.findOne({name:n}); 
    var e = "That game doesn't exist."; 
    Session.set("error", null); 
    Session.set("no_game", null); 
    if (Validation.game_exists(n)){ 
     Validation.clear(); 
     Session.set("current_game", g._id); 
     window.location.hash = ("/" + n); 
    } else { 
     Session.set("error", e) 
    } 
}; 

을 양식 및 예상대로 작동합니다. 세션을 설정하고 게임을 표시하며 URL을 변경합니다. 이것은 해당 양식에 사용되는 방법입니다

Template.entergame.events({ 
    'click input.enter-game': function(){ 
    var n = document.getElementById('enter-game-name').value; 
    enterExisting(n); 
} 
}); 

나는 그것이 세션을 설정하거나 나에게 지시하지 않는 Meteor.startup에 유사한 방법으로 같은 기능을 사용하려고하면.

Meteor.startup(function() { 
    Session.set("current_game", ""); 
    Session.set("error", null); 
    Session.set("no_game", null); 
    var n = location.hash.substring(2); 
    if (n.length === 0) { 
     window.location.hash = "#/" 
    } else { 
     enterExisting(n); 
    } 
}); 

나는 관련이있을 것으로 예상하지만, 단지의 경우 여기에 "게임에 참여"형태의 템플릿입니다하지 않습니다 당신이 'Meteor를 교체 할 경우

<template name="entergame"> 
    <div class="enter-game"> 
     {{#if error}} 
      <p class="error bad-entry"> 
      {{error}} 
      </p> 
     {{/if}} 
     <input id="enter-game-name" type="text" placeholder="Join an Existing Game!" /> 
     <input type="button" class="enter-game" value="»"> 
    </div> 
</template> 
+0

이것이 작동하지 않는 이유는 확실하지 않지만 권장 사항 : 1) http://docs.meteor.com/#session_set_default를 사용하고 2) https://github.com/tmeasday/meteor-router 또는 최신 https://github.com/EventedMind/iron-router로 클라이언트 측 라우팅을 수행하십시오. – emgee

답변

0

이 작동합니다. startup 'with'Template.entergame.created '

'Meteor.startup '은 서버용이며 템플릿이 렌더링 될 때마다 실행되지 않습니다. 이것이 모든 템플릿에 '생성 된'이벤트가있는 이유입니다.

emgee가 맞습니다. 클라이언트 측 라우팅에 iron-router을 사용해야합니다 (현재 서버 측 없음). 특히 iron-router는 렌더링시 템플릿의 기본 데이터를 설정할 수 있기 때문에 특히 그렇습니다.

0

나는이 일을하고 다른 사람들에게 유용 할 수 있기 때문에 내 자신의 질문에 대답합니다. 나는 더 이상 clunky UX를 개선하기 위해 좀 더 나아 갔지만, 현재 Gorb와 emgee 박사가 제안한 철제 라우터를 선호하여이 모든 작업을 제거 할 계획입니다. 서버에서 데이터를받는 데 오랜 시간이 걸리는 경우이 솔루션을 사용할 수 없습니다.

문제는 publish/subscribe 타임 라인과 관련되어 있습니다. 특히 적절한 템플릿을 렌더링하기 전에 구독이 서버에서 수신 될 때까지 기다릴 필요가있었습니다.

첫 번째 단계는 자동 게시를 제거하는 것입니다.

다음으로,이 같은 콜백에 URL 파싱 기능을 넣어 :이

Meteor.subscribe("games", function(){ 
     if (n.length === 0) { 
     window.location.hash = "#/" 
     $('.home').fadeIn('slow'); 
     } else { 
     enterExisting(n); 
     setTimeout(function(){$('.currentgame').fadeIn('slow')}, 1); 
    }}); 

:

여기
Meteor.startup(function() { 
    Session.set("current_game", ""); 
    Session.set("error", null); 
    Session.set("no_game", null); 
    var n = location.hash.substring(2); 
    Meteor.subscribe("dice"); 
    Meteor.subscribe("games", function(){ 
     if (n.length === 0) { 
     window.location.hash = "#/" 
     $('.home').fadeIn('slow'); 
     } else { 
     enterExisting(n); 
     setTimeout(function(){$('.currentgame').fadeIn('slow')}, 1); 
    }}); 
}); 

구독 데이터를 반환 한 후로드 할 템플릿을 결정 단지 콜백입니다 솔루션을 사용하면 페이지가로드 된 후 이 아마도이되고 데이터가로드되면 갑자기 템플릿을 전환합니다.그래서 간단한 솔루션을 추가했습니다 : 준비가되었을 때 올바른 템플릿을 변경하고 사라질 수있는 부분을 숨 깁니다. 내 CSS에서

: 위의 JS에서

.home, .currentgame {display:none;}

, 당신이 볼 다음 setTimeout에 관해서는

Meteor.subscribe("games", function(){ 
     . 
     . 
     . 
     $('.home').fadeIn('slow'); 
     . 
     . 
     . 
     setTimeout(function(){$('.currentgame').fadeIn('slow')}, 1); 
    }}); 

를, 난 정말 아무런 설명이 없다, 그러나 그것은 작동 얻었다 . (나는 그 Session.set 물건 뒤에 그 기능을 대기열에 넣는 문제라고 생각한다.)

관련 문제