Alex
Alex

Reputation: 3790

backbone js MVC semantics advice

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.

Upvotes: 2

Views: 227

Answers (4)

Alex
Alex

Reputation: 3790

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.

Upvotes: 0

pradeek
pradeek

Reputation: 22095

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.

Upvotes: 0

Eric Turner
Eric Turner

Reputation: 1824

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.

Upvotes: 1

Brian Genisio
Brian Genisio

Reputation: 48127

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.

Upvotes: 1

Related Questions