Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8625607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:49:49+00:00 2026-06-12T07:49:49+00:00

I’m working within a Javascript + BackboneJS (an MVC framework) + RequireJS framework, but

  • 0

I’m working within a Javascript + BackboneJS (an MVC framework) + RequireJS framework, but this question is somewhat OO generic.

Let me start by explaining that in Backbone, your Views are a mix of traditional Views and Controllers, and your HTML Templates are the traditional MVC Views

Been racking my head about this for a while and I’m not sure what the right/pragmatic approach should be.

I have a User object that contains user preferences (like unit system, language selection, anything else) that a lot of code depends on.

Some of my Views do most of the work without the use of templates (by using 3rd party libs, like Mapping and Graphing libs), and as such they have a dependency on the User object to take care of unit conversion, for example. I’m currently using RequireJS to manage that dependency without breaking encapsulation too much.

Some of my Views do very little work themselves, and only pass on Model data to my templating engine / templates, which do the work and DO have a dependency on the User object, again, for things like units conversion. The only way to pass this dependency into the template is by injecting it into the Model, and passing the model into the template engine.

My question is, how to best handle such a widely needed dependency?
– Create an App-wide reference/global object that is accessible everywhere? (YUK)
– Use RequireJS managed dependencies, even though it’s generally only recommended to use managed dependency loading for class/object definitions rather than concrete objects.
– Or, only ever use dependency injection, and manually pass that dependency into everything that needs it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:49:51+00:00Added an answer on June 12, 2026 at 7:49 am

    From a purely technical point of view, I would argue that commutable globals (globals that may change), especially in javascript, are dangerous and wrong. Especially since javascript is full of parts of code that get executed asynchronously. Consider the following code:

    window.loggedinuser = Users.get("Paul");
    addSomeStuffToLoggedinUser();
    window.loggedinuser = Users.get("Sam");
    doSomeOtherStuffToLoggedinUser();
    

    Now if addSomeStuffToLoggedinUser() executes asynchronously somewhere (e.g. it does an ajax call, and then another ajax call when the first one finishes), it may very well be adding stuff to the new loggedinuser (“Sam”), by the time it gets to the second ajax call. Clearly not what you want.

    Having said that, I’m even less of a supporter of having some user object that we hand around all the time from function to function, ad infinitum.

    Personally, having to choose between these two evils, I would choose a global scope for things that “very rarely change” — unless perhaps I was building a nuclear powerstation or something. So, I tend to make the logged in user available globally in my app, taking the risk that if somehow for some reason some call runs very late, and I have a situation where one user logs out and directly the other one logs in, something strange may happen. (then again, if a meteor crashes into the datacenter that hosts my app, something strange may happen as well… I’m not protecting against that either). Actually a possible solution would be to reload the whole app as soon as someone logs out.

    So, I guess it all depends on your app. One thing that makes it better (and makes you feel like you’re still getting some OO karma points) is to hide your data in some namespaced singleton:

    var myuser = MyApp.domain.LoggedinDomain.getLoggedinUser();
    doSomethingCoolWith(myuser);
    

    in stead of

    doSomethingCoolWith(window.loggedinuser);
    

    although it’s pretty much the same thing in the end…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.