내 Meteor js 앱에서 매우 흥미로운 문제가 있습니다. 템플릿의 onCreated 메서드 내에서 유성 메서드를 호출 할 때 해당 메서드 호출에 대한 콜백은 때때로 undefined로 즉시 반환됩니다. 결과. 유성우 시뮬레이션을 실행 한 결과 템플릿이 생성 되었기 때문입니다.유성 메서드 시뮬레이션 외부에서 코드를 실행하는 방법
두 질문 :
- 인가이 버그? 확실히 내가 예상했던 행동이 아닙니다.
- setTimeout과 같은 이상한 해킹을 사용하지 않고 어떻게 해결할 수 있습니까? (그런데
Meteor.setTimeout
은 메소드 시뮬레이션 내부에서 허용되지 않습니다)?
일부 코드 :
// My Template (Not my real code, just to demonstrate)
Template.saying.onCreated(() => {
var tmpl = Template.instance();
tmpl.saying = new ReactiveVar();
Meteor.call('getSaying', (err, saying) => {
// If called inside of a simulation, saying is null
tmpl.saying.set(saying);
});
});
// Assume that the above template is used in an {{each}} block
// and somewhere in my code I call this
Items.insert({});
// Because Items.insert wraps a meteor method which also runs as a
// simulation on the client, then the Template.saying.onCreated
// callback will be called in the context of an active simulation,
// which means that 'getSaying' method call will return immediately
// with undefined as the result.
로 평가됩니다 실행하면 - 당신은 서버에게 부모 템플릿이 단지에 렌더링 될 때마다 루프에서 1 회/템플릿을 호출 할거야 반응 변수를 설정하십시오. 이 작품을 만들더라도 느려질 것입니다. 서버에서 가져온 데이터를 콜렉션에 넣고 Meteor가 클라이언트에 전달하도록하십시오. –
불행히도 내가 사용해야하는 패턴이 있습니다. 각 템플릿에는 자체 토큰이 필요하며, 토큰은 즉석에서 생성되어야합니다. – cwohlman