Backbone js MVC 语义建议
我正在构建我的第一个基于backbone.js 的网站,我有一些语义问题。 我的 JS 应用程序的结构如下:
application.js
/collections/WorkspaceCollection.js
...buch of other more atomic collections
/views/WorkspaceView.js
...buch of other more atomic views
/controllers/WorkspaceController.js (extends router)
...bunch of other atomic controllers
/models/WorkspaceModel.js
...bunch of other atomic models
在我的 application.js 文件中,我实例化 Worksapce 并且它可以正常运行。
我的问题是,如果我有一堆……不……我有时需要执行的基于 MVC 的函数,可以说就像在不同地方使用的一堆数学函数一样。
简单地将它们转储到 application.js 中是否更正确,或者我应该扩展全局 Math 对象(这看起来更干净,但是嗯)
我正在寻找卫生提示:-)
[编辑]:
只是为了避免任何困惑,我有一堆数学函数,我需要它们来做一些向量数学,以及 javascript 在它自己的数学类中本身不具备的一些东西。 我在整个应用程序的不同地方使用了其中一些。 顺便说一句,如果我也有大量字符串、音频视频等功能,我猜这个问题可能适用。
I'm building my first site based on backbone.js and I have a bit of a semantics question.
My JS app is structured like so:
application.js
/collections/WorkspaceCollection.js
...buch of other more atomic collections
/views/WorkspaceView.js
...buch of other more atomic views
/controllers/WorkspaceController.js (extends router)
...bunch of other atomic controllers
/models/WorkspaceModel.js
...bunch of other atomic models
in my application.js file i instantiate the Worksapce and it goes about it's business fine.
My question is that if I have a load of sortof... no.. MVC based functions which I need to perform sometimes lets say like a buch of math functions used in various places.
Is it more correct to simply dump these into the application.js or should I look to extend the global Math object (that seems a bit cleaner but hmmm)
guess I'm looking for hygiene tips :-)
[Edit]:
Just to avoid any confusions, I have a bunch of math functions which I need to do some vector maths as well as a few things which javascript doesnt inherently have in it's own Math class.
I use some of these in different places throughout the app.
Incidentally, this question could I guess apply if I had a load of string, audio video etc functions too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我有点难以理解你在这里问的问题......
但数学函数感觉它们应该是你的模型的一部分。模型不仅仅是一堆数据对象。它包括对您的应用程序很重要的业务规则。它为您的世界建模,其中通常包含数学。视图通常不太关心数学,除非您正在做一些更多的本机绘图或动画...
如果所讨论的数学函数是通用的,那么也许您只需要一个数学实用函数?我的意思是,您可以扩展 Math 对象,但为什么要冒污染该空间的风险呢?如果您不想在模型中使用它,则可以创建自己的数学实用程序。
I am having a bit or trouble understanding what you are asking here...
But math functions feel like they should be part of your model. The model isn't just a bunch of data objects. It includes business rules that are important to you application. It models your world which often includes math. The view doesn't usually care as much about math unless you are doing some more native drawing or animation...
If the math functions in question are generic, then maybe you just need a math utility function? I mean, you CAN extend the Math object but why risk polluting that space? If you don't want it in your model then make your own math utility.
您想添加哪些数学函数?如果数学函数特定于您的模型,我会将它们添加到那里。如果它们是您在任何数学库中所期望的通用数学函数,那么扩展全局数学对我来说似乎很好。我会为其他任何事情创建一个新课程。
What math functions do you want to add? If the math functions are specific to your models I would add them there. If they are generic math functions that you would expect in any math library, extending the global Math seems fine to me. Anything else I would create a new class for.
如果它独立于您正在构建的应用程序,我会说它应该是一个单独的文件。扩展本机 Math 对象通常被认为是一种不好的做法。
If it is independent of the application you are building I'd say it should be a seperate file. Extending the native Math object is generally considered a bad practice.
我最终在运行时扩展了下划线,这可能是不对的,但是下划线的本质基本上是一组实用函数,因此将我的实用函数附加到这个命名空间是有意义的,因为它们中的很多都使用和称赞了下划线函数。
任何对此的想法也将不胜感激。
I ended up extending underscore at runtime, this probably isn't right but the nature of underscore basically being a set of utilitarian functions it made sense for my utilitarian functions to be attached to this namespace as a lot of them used and complimented underscore functions.
Any thoughts on this would be appreciated too.