在Meteor中创建一个响应式模板助手
Making a template helper reactive in Meteor
我正在构建一个聊天应用程序,在我的"新聊天"页面上我有一个联系人列表,您可以通过点击它们逐个选择(在此基础上,我应用CSS选择类并将用户id推送到一个名为'newChatters'的数组中)。
我想让这个数组对一个助手方法可用,这样我就可以显示一个响应式的名称列表,其中包含所有已添加到聊天中的用户。
我想在其中显示响应列表的模板:
<template name="newChatDetails">
<div class="contactHeader">
<h2 class="newChatHeader">{{newChatters}}</h2>
</div>
</template>
选中联系人时触发的click contactItem事件:
Template.contactsLayout.events({
'click #contactItem': function (e) {
e.preventDefault();
$(e.target).toggleClass('selected');
newChatters.push(this.username);
...
newChatters数组正在正确更新,所以到目前为止一切正常。现在我需要使{{newChatters}}响应更新。这是我尝试过的,但它不正确,不工作:
Template.newChatDetails.helpers({
newChatters: function() {
return newChatters;
}
});
我如何以及在哪里使用Deps.autorun()使此工作?我甚至需要它,因为我认为助手方法自动更新无效?
1)在定义对象的位置定义Tracker.Dependency
:
var newChatters = [];
var newChattersDep = new Tracker.Dependency();
2)在读取对象之前使用depend()
:
Template.newChatDetails.newChatters = function() {
newChattersDep.depend();
return newChatters;
};
3)使用changed()
后写:
Template.contactsLayout.events({
'click #contactItem': function(e, t) {
...
newChatters.push(...);
newChattersDep.changed();
},
});
您应该使用Session
对象。
Template.contactsLayout.events({
'click #contactItem': function (e) {
//...
newChatters.push(this.username);
Session.set('newChatters', newChatters);
}
});
和
Template.newChatDetails.helpers({
newChatters: function() {
return Session.get('newChatters');
}
});
您可以使用本地Meteor.Collection
游标作为响应性数据源:
var NewChatters = new Meteor.Collection("null");
模板:
<template name="newChatDetails">
<ul>
{{#each newChatters}}
<li>{{username}}</li>
{{/each}}
</ul>
</template>
事件:Template.contactsLayout.events({
'click #contactItem': function (e) {
NewChatters.insert({username: this.username});
}
});
助手:
Template.newChatDetails.helpers({
newChatters: function() { return NewChatters.find(); }
});
要模仿Session
的行为而不污染Session
,请使用ReactiveVar
:
Template.contactsLayout.created = function() {
this.data.newChatters = new ReactiveVar([]);
}
Template.contactsLayout.events({
'click #contactItem': function (event, template) {
...
template.data.newChatters.set(
template.data.newChatters.get().push(this.username)
);
...
然后在内部模板中,使用父响应数据源:
Template.newChatDetails.helpers({
newChatters: function() {
return Template.parentData(1).newChatters.get();
}
});
对于在2015+年寻找解决方法的人(因为该职位是2014年)。
我正在实现一个帖子向导pw_module
,我需要根据路由参数响应地更新数据:
Router.route('/new-post/:pw_module', function(){
var pwModule = this.params.pw_module;
this.render('post_new', {
data: function(){
switch (true) {
case (pwModule == 'basic-info'):
return {
title: 'Basic info'
};
break;
case (pwModule == 'itinerary'):
return {
title: 'Itinerary'
};
break;
default:
}
}
});
}, {
name: 'post.new'
});
之后在模板中执行a:
<h1>{{title}}</h1>
更新URL的导航如下所示:
<nav>
<a href="{{pathFor route='post.new' pw_module='basic-info'}}">Basic info</a>
<a href="{{pathFor route='post.new' pw_module='itinerary'}}">Itinerary</a>
</nav>
希望还能帮到一些人。
相关文章:
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- 使用来自不同异步函数的响应创建一个json对象
- 响应应包含一个对象,但得到的却是GET操作的数组
- D3.js:将一个有响应的饼图放在其父分区的中心
- 获取javascript函数中另一个javascript函数的响应
- 嵌套另一个 Ajax 请求时,Ajax 响应处理中断
- 如何将请求响应传递给另一个请求
- 有一个响应式按钮
- getList 的响应应该是一个数组,而不是一个对象或其他东西
- 使用来自AngularJS的Web API JSON响应-错误:应为和数组,但得到了一个对象
- 必须单击两次才能得到一个响应
- 响应于一个“;输入“;在<输入>领域
- 请求一个完整的响应对象,而不是紧凑的响应对象foursquare场地搜索api
- 另一个域上的ajax响应
- 当通过另一个函数调用它时,函数会给出NaN响应
- 服务器以 Status=304 响应一个浏览器的 GET 请求,以 200 响应另一个浏览器的 GET 请求
- d3:对一个类采取行动以响应一个事件
- 如何使一个声音响应一个按键在js html画布
- PHP/JS内容交换正在响应一个";1〃;毕竟?可以'找不到来源
- 下载一个文件发送响应一个POST请求通过nodejs?(没有AJAX)