PDA

View Full Version : common util methods in MVC



atul.sachan@capgemini.com
9 May 2012, 2:17 AM
In MVC application where we need to write common util methods which can be accessed by controllers and views?

Thanks,
At

friend
9 May 2012, 5:00 AM
You can create your own custom class as a Singleton, then put all of your global/utility methods in it. In your app.js, adding an Ext.require('MyApp.util.MySingletonClass') will load/construct the class so that's it's globally available for use by namespace/classname.

fschaeffer
9 May 2012, 8:13 PM
Thanks @friend, that answers a question I also recently came up with, how to store the grid cell renderers in a central place so I can reuse them throughout the app. Do have any code block for that custom class?

I would try to do it like this, is this the right way to go?



Ext.define('MyApp.util.Renderers', {
extend: ???

singleton: true,

renderThis: function () {
//doSomething
},

renderThat: function () {
//doSomeOtherThing
}
});

friend
10 May 2012, 11:13 AM
Yes, you're on the right track. And you don't have to extend anything in particular. Here's a Singleton I use to encapsulate a bunch of renderers:



Ext.define('MyApp.widgets.Renderers', {
singleton: true,

active: function(value, p, record) {
var image = null;
if (value == true) {
image = 'active.png';
} else {
image = 'inactive.png';
}
return '<img src="resources/images/' + image + '">';
},

activeOnly: function(value, p, record)
{
var result = '';
if (value == true) {
result = '<img src="resources/images/active.png">';
}
return result;
},

expired: function(value, p, record) {
var result = '';
if (value == true) {
result = true
}
return result;
},


locked: function(value, p, record) {
var result = '';
if (value) {
result = '<img src="resources/images/lock.png">';
}
return result;
},

roleQuickTip: function(value, metaData, record, rowIndex, colIndex, store) {
metaData.tdAttr = 'data-qtip="' + record.get('description') + '"';

// primary roles get highlighted.
if (record.get('topLevel') == 1) {
value = '<span class="primaryRole">' + value + '</span>';
}
return value;
}

});