什么是附加的行为流星收藏的最佳方式? [英] What's the best way to attach behavior to a Meteor Collection?

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

问题描述

在流星,当你从数据库中检索的记录,它只是一个记录。所以,如果我有一个名为集,一个可能皮毛:棕色 一口气:'臭',但它不具有树皮()方法。

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.

显然,我可以创建一些函数期望作为一个参数,然后在执行操作 。我甚至可以封装所有这些功能整合到一个单一的构造。我不是疯了这个方法,但如果有人有了这样一个干净的和明智的方式做到这一点,我洗耳恭听。

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.

另外我想过换行 Backbone.Model 。这可能是有趣的,因为保存可以被重新定义为做找到插入更新,你可以在那里定义你所有的行为为好,但我已经阅读这种类型的东西通常不鼓励。

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.

修改

另一个答案创建集合时,使用变换属性建议。虽然我肯定preferring的东西,我并不真的需要打造出自己,这个功能增加了很多的可能性,我希望这是对流星的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.

<一个href=\"https://medium.com/@davidjwoody/meteors-new-feature-will-transform-your-mongo-collections-5aa02d00e41b\">Here's一个博客帖子解释如何使用变换属性。

此外, minimongoid现已如流星包,我仍然使用它。它有一个确认和宣布关系的支持。我已经添加了一些功能,这个包为好,因此,如果主板有很多碎片, board.pieces()创建(属性)将持续一个新的记录与给定属性,并会自动与板相关联。我见过的各种解决方案,这似乎是最COM prehensive。

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叶蝉这是ActiveRecord的喜欢和相当不错,在CoffeeScript中: 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

还有由汤姆·科尔曼提出了社会多数民众赞成包配车型非常有帮助: https://开头github上。 COM / tmeasday /流星车型的,则可能需要陨石将其添加为一个包。

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.

当然,如你所说骨干。我个人用js原型,但不是每个人都可能是舒适和他们在一起,我只是用他们,所以它很容易,当流星的模型系统是出于转型,它也很容易,没有太多包添加客户端和服务器之间共享。

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.

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

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