3

i am a newbie to web application development. i have a backbone model named LoginModel. I want to create an object of it and make it globally accessible from any Backbone View which will be loaded dynamically. Here is my model..

define(['underscore', 'backbone'], function(_, Backbone) {
    LoginModel = Backbone.Model.extend({
        initialize: function(){ },
        defaults:{
            userName: 'undefined',
            userID: 'undefined'
        },
        urlRoot: 'https://xxxx.xx.xxxxxx',
        parse: function(response, options){
            return{
                userName: response.userName,
                userId:response.userId
            };
        }
    });
});
gion_13
  • 41,171
  • 10
  • 96
  • 108
yokks
  • 5,683
  • 9
  • 41
  • 48
  • fyi, your `LoginModel` is already global and because you forgot to put the `var` declaration before the variable name. – gion_13 Jun 05 '13 at 05:49
  • @gion_13 So will it work without initializing it again in above case ? how to call parse method for LoginModel in above case ? – Sandip Armal Patil Jan 28 '14 at 13:41
  • I don't think I'm getting what you want. You just instantiate the model (`var myLoginModelInstance = new LoginModel()`) and then call the `parse` method (`myLoginModelInstance.parse()`). – gion_13 Jan 28 '14 at 14:32

3 Answers3

5

You could pin the newly created object to an already existing global object that you're using, such as Backbone :

Backbone.Model.definitions = {
    loginModel : Backbone.Model.extend({...})
}

and the use it as this :

new View({model : Backbone.Model.definitions.loignModel});

It may not be the shortest way, but it's cleaner than polluting the global namespace with different variables.

gion_13
  • 41,171
  • 10
  • 96
  • 108
1

Rather than attaching to the Backbone.Model prototype or to the global window scope, I find it effective to attach it to a separate app object (provided by backbone-boilerplate and most other implementations).

You app object then can be required into any other module that requires access to the current user context.

app/app.js

define(function(require, exports, module) {
    "use strict";

    var app = module.exports;

    // Initialize configuration and context objects
    app.root = "/";
    app.currentUser = null; //will be initialized in app/main.js
});

app/main.js

require(["config"], function() {
    require(["app", "LoginModel", "router"], function(app, models, Router) {
        app.currentUser = new LoginModel();
        app.currentUser.fetch();

        app.router = new Router();
    })
});
Mike Griffith
  • 1,201
  • 11
  • 17
-1

Just create a global instance of the model, for example

var globalLoginModel = new LoginModel();

or

window.globalLoginModel = new LoginModel();

Then just pass it into any view that you want to use it in, for example

new View1({ model : globalLoginModel });
new View2({ model : globalLoginModel });
new View3({ model : globalLoginModel });

That should be it.

D

dcarson
  • 2,853
  • 1
  • 25
  • 34