替换Template.my template.render回调在事件触发后重新渲染

时间:2014-03-29 22:39:16

标签: javascript events meteor

在Meteor 0.8.0之后,当模板插入DOM时,rendered回调仅执行一次。这会导致依赖于先前渲染功能的代码出现问题。

我尝试过以下解决方案,这会给我一些Deps错误。

创建一个函数,然后在rendered回调中以及在事件触发时调用它。

var update_post thumbs = function() { stuff... }

然后我在模板事件中调用它

Template.my_template.events = { 'click input' : function(event) { update_post_thumbs(); } }

以及渲染回调

Template.my_template.rendered = function() { update_post_thumbs(); }

每当呈现模板时,我都会收到以下错误:

[15:36:28.366] "Exception from Deps afterFlush function function: update_post_thumbs@http://localhost:3000/client/js/answer_submit.js?e5c5233b74e520c0941483e44f4b390f682707f8:98
Template.post_action_bar.rendered@http://localhost:3000/client/js/answer_submit.js?e5c5233b74e520c0941483e44f4b390f682707f8:199
UI.Component.notifyParented/<@http://localhost:3000/packages/ui.js?b523ef986d3d39671bcb40319d0df8982acacfe8:424
.flush@http://localhost:3000/packages/deps.js?7afb832ce6e6c89421fa70dc066201f16f9b9105:331
"

每当事件被触发时,我都会收到此错误:

[15:37:13.978] "Exception in delivering result of invoking 'update_vote_count_question': update_post_thumbs@http://localhost:3000/client/js/answer_submit.js?e5c5233b74e520c0941483e44f4b390f682707f8:98
Template.post_action_bar.events["click .post-thumbs"]/<@http://localhost:3000/client/js/answer_submit.js?e5c5233b74e520c0941483e44f4b390f682707f8:179
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?148e9381d225ecad703f4b858769b636ff7a2537:822
._maybeInvokeCallback@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:3505
.dataVisible@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:3534
._runAfterUpdateCallbacks/<@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:4217
_.forEach@http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156
._runAfterUpdateCallbacks@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:4216
._livedata_data@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:4206
Connection/onMessage@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:3375
._launchConnection/self.socket.onmessage/<@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:2705
_.forEach@http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156
._launchConnection/self.socket.onmessage@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:2704
SockJS</REventTarget.prototype.dispatchEvent@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:150
SockJS</SockJS.prototype._dispatchMessage@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:1135
SockJS</SockJS.prototype._didMessage@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:1193
SockJS</SockJS.websocket/that.ws.onmessage@http://localhost:3000/packages/livedata.js?7f11e3eaafcbe13d80ab0fb510d25d9595e78de2:1340
"

该事件在服务器上运行一个函数,我等待它的结果然后我触发update_post_thumbs()

数据在数据库中正确更新,因此在服务器上触发的功能运行正常。

0 个答案:

没有答案