PDA

View Full Version : Controller lifecycle and instance variables in MVC



misterblinky
17 Nov 2011, 8:00 AM
I must be missing something simple. I have a controller in which I instantiate the view it controls and assign it to an instance variable within the controller. Pretty standard so far, i think. However, when i reference the instance variable in various methods within the controller, the controller's instance variable no longer has the handle on the object i assigned to it previously.

I've tried instantiating it in init: and in onLaunch: and in individual methods and the instance never seems to persist. Here's some abbreviated code:


Ext.define('Mything.controller.LoginController', {
extend: 'Ext.app.Controller',

loginView: {}, // whether I have this variable here or not doesn't appear to matter

init: function() {
console.log("LoginController: in init");
this.loginView = this.getView('LoginView').create(); // get and instantiate the view
console.log(this.loginView); // it's correctly instantiated here

this.application.on({
'login-force': this.showLogin
});

},

onLaunch: function() {
// If i do the same thing as shown in init() here, doesn't matter -- same behavior
},

showLogin: function() {

console.log(this.loginView); // here it is undefined
}
});


How do i instantiate the view in such a way that it persists across calls to the controller's methods? I don't want to have to instantiate it repeatedly -- or is that the practice?

thanks

mitchellsimoens
17 Nov 2011, 8:48 AM
In you showLogin method, is the scope (this) to the controller or to this.application?

Also, what framework are you using so I can move this thread to the appropriate forum?

misterblinky
17 Nov 2011, 12:28 PM
doh. Yep. Scope issue. So what's the standard practice to avoid scope confusion? To declare a "self: this," at the beginning of the class definition? What's best practice?

And sorry about the wrong forum. I'll get the hang of this eventually. I was aiming at Ext JS 4 forum.

thank you sir.

mitchellsimoens
17 Nov 2011, 12:31 PM
Try this:


this.application.on({
scope : this,
'login-force': this.showLogin
});

misterblinky
18 Nov 2011, 7:18 AM
thank you