문제

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?

도움이 되었습니까?

해결책

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
    }

});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top