您可以使用以下方式渲染模板data
,它基本上只是插入到页面时传递给它的对象。
数据可能只是在应用程序中使用的关键Session
for editState
.
例如,渲染模板Template.form({editStateKey:'editState-topForm'})
你可以制作一个车把助手,例如,
Handlebars.registerHelper('formWithOptions',
function(editStateKey){
return Template.form({editStateKey:editStateKey})
});
然后将其插入到您的模板中
{{{formWithOptions 'editState-topForm'}}}
(注意三重{
, }
)
接下来,更改引用Session.x('editState')
to Session.x(this.editStateKey)
/ Session.x(this.data.editStateKey)
Template.form.helpers({
editState: function() {
return Session.get(this.editStateKey);
}
});
// Rendered
Template.form.rendered = function(e){
var $this = $(this.firstNode);
var formField = this.find('.form-field');
if (Session.get(this.data.editStateKey)) formField.focus();
};
// Event map
Template.form.events({
'click .edit-btn' : function (e, template) {
e.preventDefault();
Session.set(this.editStateKey, "is-editing");
},
'click .cancel-btn' : function (e, template) {
e.preventDefault();
Session.set(this.editStateKey, null);
},
});
注意:如果您正在使用iron-router
它有额外的 api 用于传递data
到模板。
注2:在meteor 1.0中应该有更好的支持编写您自己的小部件。这应该可以更好地控制此类事情。