PDA

View Full Version : Getting a reference to current view from controller



mmmilo
3 Oct 2012, 6:36 AM
Hi,

Is there a way to get the current View in a Controller *without* setting a reference for it?


Ext.define('myapp.controller.ListingController', {
extend: 'myapp.CommonController',

config: {

views: ['ListingView'],

refs: {
main: 'ListingView',
},


}
},


launch: function() {
var bar = this.getMain().up('navigationview').getNavigationBar();
bar.hide();
},


});



In my example I have a View wrapped by a NavigationView. I want to hide the titlebar in the NavigationView. This code works fine.

However if I want to use it elsewhere, I need to add a new ref, and change it accordingly, since getMain() is the important part here. That kind of defeats the purpose of DRY principles.

What I'd *like* to do is to be able to create a generic hideTitleBar() function to CommonController (which just extends Ext.app.Controller), so I could just put:


launch: function() {
this.hideTitleBar();
},


Is there something I'm missing?

Thanks!

SebasSP
4 Oct 2012, 4:10 AM
You can query down from Ext.Viewport

Something like this:

var navviews = Ext.Viewport.query('navigationview');
for(var i=0, l=navviews.length; i<l; i++){
navviews[i].getNavigationBar().hide();
}

leandroafonso
30 Nov 2012, 9:56 AM
I had a likelly context and make a extends class to Ext.controller.Controller on controller directory like as:



Ext.define('TecFoodApp.controller.Controller', { extend: 'Ext.app.Controller', activeDataStore: null, backView: null, slideLeftTransition: { type: "slide", direction: "left" }, slideRightTransition: { type: 'slide', direction: 'right' }, onBackToCommand:function(view){ Ext.Viewport.setActiveItem(view); Ext.Viewport.animateActiveItem(view, this.SlideRightTransition); }, onSaveCommand: function (form) { var record = form.getRecord(); var values = form.getValues(); if (values.id > 0){ record.set(values); } else{ record = this.activeDataStore.getModel(); record.set(values); this.activeDataStore.add(record); novo = true; } this.activeDataStore.sync(); if (novo){ //faz reload para atualziar this.activeDataStore.load(); } this.onBackToCommand(this.backView); }, onSetActiveDataStore:function(cmp){ this.activeDataStore = cmp.getStore(); console.log("activeDataStore => " + this.activeDataStore.getStoreId()); }, onSetBackView:function(cmp){ this.backView = cmp.getId(); console.log("backView => " + this.backView); }})

This class add a new method onBackToCommand wich opens a view passed as argument. note, backView property defined by me on this class.

The views can now call the backtoview passing theirs name that maybe another extension on views that is a new challenge becouse view are a lot of components.

to use my personalized controller may be like:



Ext.define('TecFoodApp.controller.OperadorController', {
extend: 'TecFoodApp.controller.Controller', stores:["Operadores","Processos"], models:["Operador"], config: { refs: { operadorListarView: "operadorlistarview", operadorCadastrarView: "operadorcadastrarview" }, control: { operadorListarView: { scope: this, backToCommand: "onBackToCommand", itemtap: "onItemTapCommand", setActiveDataStore:"onSetActiveDataStore", setBackView: "onSetBackView" }, operadorCadastrarView: { scope:this, backToCommand: "onBackToCommand", saveCommand: "onSaveCommand" } } },....dropped.....

worth considering the example my little experience in frame, I'm learning some things from architecture to develop something more solid.