简体   繁体   English

如何通过Meteor.js模板传播数据库ID?

[英]How to propagate a db id through a Meteor.js Template?

My question touches a common scenario with web apps: you have some kind of db items that map to some kind of sets of DOM items. 我的问题触及Web应用程序的一个常见情况:您有某种类型的数据库项目映射到某种类型的DOM项目。 Eg a JSON object that maps to a form with some fields. 例如,一个JSON对象映射到带有某些字段的form

Now imagine that in different locations / on different events inside your form you need that db id. 现在想象一下,在表单中的不同位置/不同事件上,您需要该数据库ID。 On certain levels (in certain data contexts) the id will be available as this._id in Meteor events, but not in all since sub- #with statements might overwrite the data context. 在某些级别上(在某些数据上下文中),在Meteor事件中,此ID可以作为this._id使用,但在所有情况下都不可用,因为sub #with语句可能会覆盖数据上下文。

What I do currently in those cases is I set in the template containing the form <form id="{{_id}}" ...> and then in events where this._id is not available I get it via $(this).closest('form').id . 在这些情况下,我目前要做的是在包含形式<form id="{{_id}}" ...>的模板中进行设置,然后在无法使用this._id事件中,通过$(this).closest('form').id获取它$(this).closest('form').id This works. 这可行。

But my question is: is there a better way? 但是我的问题是:有没有更好的方法? how would you do it with Meteor? 你会怎么用流星呢?

This is something a lot of us are struggling with... gotta keep that DOM clean amirite? 这是我们很多人都在努力的事情……必须保持DOM干净的纯洁?

  1. If your DOM hierarchy is predictable, you can use Template.parentData(n) to get the parent data context. 如果您的DOM层次结构是可预测的,则可以使用Template.parentData(n)获取父数据上下文。

  2. Alternatively, if there's no relation to the context you need, use Blaze.getData(el) . 或者,如果与您需要的上下文没有关系,请使用Blaze.getData(el) I have a feeling this will become your new best friend. 我觉得这将成为您的新好朋友。 I know as soon as I stumbled upon it I felt like I was cheating it was so easy. 我知道当我偶然发现它的时候,我觉得自己在欺骗它是如此容易。 Here's my favorite use for it (in an event, iterating over all input fields): var checkFields = t.findAll('input'); var allGood = true; for (var i = 0; i < checkFields.length; i++) { if (!simplyValid.validateField(Blaze.getData(checkFields[i]), checkFields[i].value)) allGood = false; } 这是我最喜欢的用法(在一个事件中,遍历所有输入字段): var checkFields = t.findAll('input'); var allGood = true; for (var i = 0; i < checkFields.length; i++) { if (!simplyValid.validateField(Blaze.getData(checkFields[i]), checkFields[i].value)) allGood = false; } var checkFields = t.findAll('input'); var allGood = true; for (var i = 0; i < checkFields.length; i++) { if (!simplyValid.validateField(Blaze.getData(checkFields[i]), checkFields[i].value)) allGood = false; }

  3. Next, if your top-most module has it's own route, you can store it as an iron router param. 接下来,如果最顶层的模块具有自己的路由,则可以将其存储为铁路由器参数。 This is nice because it sticks the id in the url for easy sharing, I personally like clean urls, but other folks love this. 很好,因为它会将id在url中以便于共享,我个人喜欢干净的url,但是其他人都喜欢。

  4. Finally, my own pattern is a module-scoped object. 最后,我自己的模式是模块范围的对象。 In my case, for each route, in the onCreated I set my object window.M = {} . 就我而言,对于每条路线,在onCreated设置对象window.M = {} Then, I fill it up with local collections, a reactive dictionary, and anything else I need. 然后,我用本地集合,反应式字典以及我需要的其他任何东西填充它。 I clear it onDestroyed . 我在onDestroyed清除它。 Sure, it's technically a global, but the scope dies with the module. 当然,从技术上讲,它是全局的,但是作用域随模块而消失。

  5. Eliminate the #with : {{#with collection='clients'}} {{>subTemplate}} {{/with}} Becomes {{>subTemplate collection= 'clients'}} 消除#with{{#with collection='clients'}} {{>subTemplate}} {{/with}} Becomes {{>subTemplate collection= 'clients'}}

  6. Assign the #with to an object within the context: ``` {{#with collection='clients'}} {{>subTemplate name='foobar'}} //you lose the with context here {{/with}} Becomes {{>subTemplate collection=this name='foobar'}} //you keep context inside an object #with分配给#with中的对象:```{{## with collection ='clients'}} {{> subTemplate name ='foobar'}} //您在这里丢失with上下文{{/ with}}成为{{> subTemplate collection = this name ='foobar'}} // //将上下文保留在对象中

  7. Access initial parent data via spacebars' double dots & reference that context all the way down {{>subTemplate parent=..}} ``` 通过空格键的双点访问初始父数据,并始终引用上下文{{>subTemplate parent=..}} ```

Those are all the patterns I currently use to keep a clean DOM. 这些就是我目前用来保持干净DOM的所有模式。 Will be happy to edit if someone else suggests another. 如果有人建议另一个人,将很乐意进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM