2

我想在更改为其他视图之前在视图上执行一个方法。例如。

var HomeView = Backbone.View.extend({  
......,   
close: function() {

}
});

var LoginView = Backbone.View.extend({  
......    
}
});

如果我当前的视图是“HomeView”并且如果我要切换到“LoginView”,我需要在切换到 LoginView 之前在 HomeView 中执行关闭功能。有办法吗?

换句话说,我的视图应该在更改之前自动执行关闭功能。

我看过一些stackoverflow以前的帖子。在这种情况下,它们似乎已过时或无用。

4

3 回答 3

3

如果您使用路由器在视图之间切换:

var HomeView = Backbone.View.extend({
    initialize : function(){
        /*
            router here references a global router object
            "route" event is triggered when any route has been matched; 
            if you want to call the close function only when navigating to login view you may use something like "route:login"
        */
        this.listenTo( router , "route", this.close );
    },
    close: function( router, route ) {
            /* becaouse route event will be called every time the route changes. 
               it will also be called when navigating to the HomeView. 
               use the route argument to filter this event out 
            */
            if( route != 'home' ){
               // close
            }
    }
    ....
});
于 2013-05-13T06:16:05.013 回答
0
**HomeView.js**
var HomeView = Backbone.View.extend({  

});

var LoginView = Backbone.View.extend({  
......    
}
});

function close()
{
 //code
}

现在在路由器中或在调用另一个视图之前只需调用 close() 方法。

希望有帮助......

于 2013-05-13T06:15:48.187 回答
-1
**HomeView.js**
var HomeView = Backbone.View.extend({  
initialize: function(){
},
close: function(){ //code }
});

var LoginView = Backbone.View.extend({  
 initialize: function(){
  (new HomeView).close();
  //code
 }
}
});
于 2013-05-13T06:46:25.900 回答