Question

While looking for solution I came across one example here where all other views are extended from a baseview

In base view

 define(function(require) {
   var AppView = Backbone.View.extend({
   requireLogin: true
 });

 return AppView;
});

And in the extended views which don't need authenticaton put

  requireLogin: false

But couldn't find where and how is requireLogin implemented.

Also, in another similar question the last answer suggests the same but doesn't talk about how to implement it.

How can I use this?

Was it helpful?

Solution

Ok, first, I'm going to assume that you did not use a plugin like super, so let me suggest it: https://github.com/lukasolson/Backbone-Super

We want super to communicate with the parent view. Said that, do something like this: (for simplicity I would not use define)

/*
  Whatever is your process for login, it should inject 
  this object with a true flag when logged
*/
var CredentialsCollector = {
    logged : false
};

var AppView = Backbone.View.extend({

    requireLogin: true,

    render : function(){
        if( requireLogin && !CredentialsCollector.logged ){
            //do a redirect to your login module
        }
    }

});
var SomeView = AppView .extend({
    render : function(){
        /*
          this call the parent render to check the login; if required and not
          logged it automatically make the redirection
        */
        this._super();
        //<- here do your normal render stuff
    }

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top