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 8004009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:48:11+00:00 2026-06-04T16:48:11+00:00

I want to create a UserSession model, which loads and saves the session ID

  • 0

I want to create a UserSession model, which loads and saves the session ID into a cookie using the jQuery cookie plugin.

This is my code for my UserSession model module:

define(['jQuery', 'Underscore', 'Backbone'],
function($, _, Backbone){

    var UserSession = Backbone.Model.extend({
        defaults: {
            'accessToken': null,
            'userId': null
        },
        initialize: function(){
            this.load();
        },
        authenticated: function(){
            return Boolean(this.get('accessToken'));
        },
        save: function(authHash){
            $.cookie('userId', authHash.id);
            $.cookie('accessToken', authHash.accessToken);
        },
        load: function(){
            this.userId = $.cookie('userId');
            this.accessToken = $.cookie('accessToken');
        }
    })

    return UserSession;
});

But lets say I want to access it into my login view:

define(['jQuery', 'Underscore', 'Backbone', 'text!templates/login.html', 'models/UserLogin', 'models/UserSession'],
function($, _, Backbone, loginTemplate, UserLogin, UserSession){

    var LoginView = Backbone.View.extend({
        model: new UserLogin,
        el: $('#screen'),
        events: {
            'submit #frm-login': 'login'
        },
        login: function(e){
            e.preventDefault(); // Lets not actually submit.

            this.model.set({
                'username': $('#login-username').val(),
                'password': $('#login-password').val()
            });

            this.model.save(null, {
                success: function(nextModel, response){
                    // Do something here with UserSession model
                },
                error: function(){
                }
            });
        },

        render: function(){
            $(this.el).html(_.template(loginTemplate, {}));
            return this;
        }
    });

    return new LoginView;
});

The thing is each time I access the UserSession model in modules (see the model.save success call back function) it uses default values, so I need to have some kind of singleton instance of the UserSession model, how can I do this?

My first idea was to use the app namespace so in our main.js (first main module that gets loaded) and there initialize the UserSession module, and each time another module access that module, the require the main module which has a object that returns the UserSession instance.

How can this be done best?

Thanks

  • 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-04T16:48:13+00:00Added an answer on June 4, 2026 at 4:48 pm

    Assuming you want a single global UserSession across your entire application, I would just return an instantiated UserSession from the UserSession module. That way you’ll get the same object back whenever you use UserSession in a define call. For example

    define(['jQuery', 'Underscore', 'Backbone'],
    function($, _, Backbone){
        var UserSession = Backbone.View.extend({ ... });
    
        return new UserSession();
    });
    

    Then elsewhere:

    define(['UserSession'],
    function( UserSession ){
        // UserSession in here will be the singleton you created in the UserSession module
        alert( UserSession.authenticated() );
    });
    

    Require.js should only run the UserSession module once regardless of how many times it’s used elsewhere, so the UserSession object you instantiate in there is effectively a singleton.

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

Sidebar

Related Questions

I want create wordpress website into which I want create user management... That means
I want create module which update list of usb devices automatically (not only mass
I want to create a new application (which I will sell) that uses web
I want create a ModelForm class where model is a parameter passed from the
I want create a DataSet class which is basically a list of samples. But
I want create subdomains like this: domain.com/type/city An examples: domain.com/restaurants/new_york domain.com/hotels/new_york domain.com/restaurants/chicago I have
I want to implement something like this using JSF.(part of search screen) More and
i want create something like link_to which can link to request for permission in
I want create a html5 audio in dynamic and play it,here is the code:
i use cakephp and i want create login element . this layout work as

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.