将行为附加到 Meteor 集合的最佳方法是什么? [英] What's the best way to attach behavior to a Meteor Collection?

查看:16
本文介绍了将行为附加到 Meteor 集合的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Meteor 中,当您从数据库中检索记录时,它只是一条记录.因此,如果我有一个名为 Dogs 的集合,dog 可能有 fur: 'brown'breath: 'stinky',但它没有 bark() 方法.

In Meteor, when you retrieve a record from a database, it's only a record. So if I have a collection called Dogs, a dog might have fur: 'brown' or breath: 'stinky', but it doesn't have a bark() method.

显然,我可以创建一些期望 dog 作为参数的函数,然后对该 dog 执行操作.我什至可以将所有这些函数封装到一个构造函数中.我对这种方法并不疯狂,但如果有人有一种干净而明智的方法来做到这一点,我会全神贯注.

Obviously, I could create some functions that expect a dog as an argument and then perform operations on that dog. I could even encapsulate all these functions into a single constructor. I'm not crazy about this approach, but if someone's got a clean and sensible way to do it, I'm all ears.

我的另一个想法是将 dog 包装在 Backbone.Model 中.这可能很有趣,因为 fetchsave 可以重新定义为 findinsertupdate,你也可以在那里定义你的所有行为,但我读到这种类型的事情通常是不鼓励的.

Another thought I had was to wrap the dog in a Backbone.Model. This could be interesting, as fetch and save could be redefined to do find and insert or update, and you can define all your behavior there as well, but I've read that this type of thing is generally discouraged.

有正确的方法吗?是否有一个 Meteor.Model 正式在工作中?其他人是如何解决这个问题的?

Is there a right way to do it? Is there a Meteor.Model officially in the works? How are others solving this problem?

编辑

对于那些在接受答案一年多之后才提出这个问题的人:在进行此编辑时,我正在使用 Exygy 的minimongoid mrt 包,它对haihappen 的版本有一些改进,这在链接到的博客文章中提到通过接受的答案.

For those coming to this question over a year after the accepted answer: At the time of this edit I am using Exygy's minimongoid mrt package, which has some improvements to haihappen's version that's mentioned in the blog post linked to by the accepted answer.

我目前正在为存储库做出贡献,以使结果集更像关系.希望其他人能够使用它并愿意贡献有用的功能.

I'm currently contributing to the repository to make the result sets more relation-like. Hopefully others get use out of it and feel inclined to contribute helpful functionality.

编辑

另一个答案建议在创建集合时使用 transform 属性.虽然我绝对更喜欢我不需要自己构建的东西,但这个功能增加了很多可能性,我希望任何正在为 Meteor 开发 ORM 的团队都能够从核心利用这一点.

Another answer suggested using the transform property when creating the collection. While I'm definitely preferring something that I don't really need to build out myself, this feature adds a lot of possibilities, and I would hope that any teams that are working on an ORM for Meteor would take advantage of this at the core.

这是一篇博文 解释如何使用 transform 属性.

Here's a blog post explaining how to use the transform property.

此外,minimongoid 现在作为 Meteor 包提供,我仍在使用它.它支持验证和声明关系.我也给这个包添加了一些功能,所以如果一个板子有很多块,board.pieces().create(attributes) 将保留一个新的 piece 记录与给定的 attributes 并会自动与 board 关联.在我见过的各种解决方案中,这似乎是最全面的.

Also, minimongoid is now available as a Meteor package, and I am still using it. It has support for validation and for declaring relationships. I've added some functionality to this package as well, so if a board has many pieces, board.pieces().create(attributes) will persist a new piece record with the given attributes and will automatically associate with the board. Of the various solutions I've seen, this seems to be the most comprehensive.

推荐答案

虽然可能存在 正在开发的官方模型系统 现在你可以做一些事情:

While there might be an official model system in the works there are some things you can do now:

Mario Uhler 有一个像 activerecord 一样,相当不错,在咖啡脚本中:https://coderwall.com/p/_q9b1w

There is one by Mario Uhler which is activerecord like and quite nice, in coffeescript: https://coderwall.com/p/_q9b1w

还有一个由 Tom Coleman 制作的社区包,它对模型非常有帮助:https://github.com/tmeasday/meteor-models,您可能需要 meteorite 将其添加为包.

There is also a community package made by Tom Coleman thats very helpful with models: https://github.com/tmeasday/meteor-models, you might need meteorite to add it as a package.

当然,正如你建议的 Backbone.我个人使用 js 原型,但不是每个人都可能对它们感到满意,我只是使用它们以便在meteor 的模型系统出来时很容易过渡,也很容易在客户端和服务器之间共享,无需添加太多包.

And of course as you suggested Backbone. I personally use js prototypes but not everyone may be comfy with them, I just used them so its easy to transition when meteor's model system is out, its also easy to share between the client and server without too many packages to add.

这篇关于将行为附加到 Meteor 集合的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆