2015-04-05 65 views
0

Session.set('coursesReady', false);在啓動。全局模板助手內部的會話對象

UPDATE:

我把它做成一個簡單的問題。考慮下面的代碼。 內部router.js

Router.route('/', function() { 
    Meteor.subscribe("courses", function() { 
    console.log("data ready") 
    Session.set("coursesReady", true); 
    }); 
} 

和主模板內Main.js

Template.Main.rendered = function() { 
    if (Session.get('coursesReady')) { 
     console.log("inject success"); 
     Meteor.typeahead.inject(); 
    } 

消息 「注入成功」 之後的 「數據就緒」 被打印未打印。反應性如何在這裏不起作用?

回答

1

反應性「沒有工作」,因爲rendered只執行一次(它沒有反應)。你需要換你的會話檢查一個template autorun的內部,以便讓他們重新估計:

Template.Main.rendered = function() { 
    this.autorun(function() { 
    if (Session.get('coursesReady')) { 
     console.log("inject success"); 
     Meteor.typeahead.inject(); 
    } 
    }); 
}; 

可能是一個更好的解決辦法是wait on,如果你想確保你的數據在訂閱加載渲染之前模板。

Router.route('/', { 
    // this template will be rendered until the subscriptions are ready 
    loadingTemplate: 'loading', 

    waitOn: function() { 
    // return one handle, a function, or an array 
    return Meteor.subscribe('courses'); 
    }, 

    action: function() { 
    this.render('Main'); 
    } 
}); 

現在你可以rendered只是這樣做:

Template.Main.rendered = function() { 
    Meteor.typeahead.inject(); 
}; 

不要忘記添加加載模板。

+0

我在哪裏包括'Meteor.typeahead.inject();'在這種情況下?我嘗試了幾次使用'waitOn',但似乎'waitOn'總是需要'Meteor.subscribe('courses');'返回。另外,我很好奇爲什麼反應性在這種情況下不起作用,無論訂閱問題如何。 – 2015-04-06 02:35:02

+0

如果我只有訂閱,是'waitOn'比'subscribe('some_data',callback)'裏面的回調函數更好/更快嗎?我有我的另一個問題中描述的一些性能問題,http://stackoverflow.com/questions/29466051/how-to-debug-performance-issue-optimize-your-meteor-app – 2015-04-06 06:47:47

+0

鑑於您擁有的文件數量是異常高的(這是你的另一個問題的話題),我會說無論你做什麼都需要很長時間。答案取決於你的UX應該是什麼。如果用戶很快看到頁面,但看不到數據很重要,我建議使用[模板訂閱](https://docs.meteor.com/#/full/Blaze-TemplateInstance-subscribe)。如果用戶看到加載頁面會更好,請使用'waitOn'。 – 2015-04-06 14:08:02

0

爲您解決問題

Template.registerHelper("course_data", function() { 
    console.log("course_data helper is called");  
    if (Session.get('coursesReady')) { 
     var courses = Courses.find().fetch(); 
     var result = [ {       **Changed** 
      name: 'course-info1', 
      valueKey: 'titleLong', 
      local: function() { 
      return Courses.find().fetch(); 
      }, 
      template: 'Course' 
     }]; 
     Session.set('courseResult', result); **New line** 
     return Session.get('courseResult'); **New line** 
     , 

說明

答案是功能需要與反應相關的輔助,才能回報的火焰,模板渲染,以知道什麼時候放棄。

非反應性(不在DOM改變爲值的變化)

Template.Main.helpers({ 
    course_data: UI._globalHelpers.course_data ** Not reactive 
    }); 

本質:UI._globalHelpers.course_data返回對象的排列構成的非反應性:

return [ 
     { 
      name: 'course-info1', 
      valueKey: 'titleLong', 
      local: function() { 
      return Courses.find().fetch(); 
      }, 
      template: 'Course' 
     }, 

反應式

從Meteor文檔: http://docs.meteor.com/#/full/template_helpers

Template.myTemplate.helpers({ 
    foo: function() { 
    return Session.get("foo"); ** Reactive 
    } 
}); 

將Session.get函數返回給Blaze是被動的;因此,模板將隨着數值的變化而改變。

+0

不幸的是,這並沒有解決問題。 – 2015-04-05 22:30:01