我希望让我的流星订阅动态/被动。现在,我有一个页面根据模板ID订阅了一组模板:
Meteor.subscribe(' templates',templateId)
在同一页面上,我有一个模板名称和ID的下拉列表:
<p><label>Select Template</label></p>
<select id="templateSelect" name="templateSelect">
<option disabled selected> Select Template </option>
{{#each orderTemplates}}
<option value="{{this._id}}">{{this.templateName}}</option>
{{/each}}
</select>
我希望订阅变量templateId
等于我选择的模板的选项值。有人有主意吗?我真的不知道从哪一个开始...
谢谢!
答案 0 :(得分:4)
如果您想要模板订阅,则应遵循以下策略:
Template.myTemplate.events({
'change #templateSelect': function (event, template) {
// extract the value/id from the selected option
var value = $(event.target).val();
// update the templateId - whis will cause the autorun to execute again
template.templateId.set(value);
}
});
Template.myTemplate.onCreated(function() {
var self = this;
// store the currently selected template id as a reactive variable
var templateId = new ReactiveVar;
// this will rerun whenever templateId changes
this.autorun(function() {
// Subscribe for the current templateId (only if one is selected). Note this
// will automatically clean up any previously subscribed data and it will
// also stop all subscriptions when this template is destroyed.
if (templateId.get())
self.subscribe('orderTemplateShow', templateId.get());
});
});
推荐阅读:
答案 1 :(得分:2)
要获取所选选项的 id ,请使用jQuery选择器按ID进行选择,然后选择该菜单的.val()
:
var templateId = $('select[id="templateSelect"]').val();
Meteor.subscribe('templates', templateId);