PDA

View Full Version : best approach for code that's hard to separate into property MVC setup



jep
1 Nov 2012, 1:26 PM
I have a view that's a chart. It has an X axis that can be grouped many different ways - day, week or month. So that chart looks something like this:


{
xtype:'chart',
axes:[
{
type:'category',
position:'bottom',
renderer:function (x) {
var date = new Date(x);
if (this.getGrouping() === 'D') {
return Ext.Date.format(date, 'M d');
}
else if (this.getGrouping() === 'W') {
return Ext.Date.format(date, 'M d') + '-' +
Ext.Date.format(Ext.Date.add(date, Ext.Date.DAY, 7), 'd');
}
else if (this.getGrouping() === 'M') {
return Ext.Date.format(date, 'M');
}
},
...
}


Ideally, this.getGrouping() would return the grouping. But the grouping is another part of the program, set in currently toggled button of a SegmentedButton elsewhere. And in that renderer, "this" refers to the chart.

I'm trying to decide the best way to handle these kinds of situations. Oh, I know all kinds of ways I could get around this. I could use a global variable and have both the SegmentedButton event (via the controller) update it and then it would be available to the renderer. Similarly, I could subclass the chart and add a grouping property.

I could fire an event that I pass an object. The event handler in the controller then sets a property of the object and I use that to know what to do in the renderer. It could even make the code in the view very thin, just reading the entire string out of the event object. This seems to be the one I see Mitchell recommending, but in this case it might be overkill and add a lot of overhead to drawing the axes.

Or, the one I'm leaning towards, I could hook the chart initialize function and add the renderer there with axes.setRenderer. I tried it and it works. It allows me to have all the actual code in the controller. Edit: Making sure to use axis.setRenderer(Ext.function.bind(this.renderXAxis, this)) to bind the renderer function to the controller.

Would anyone recommend anything different? What are your thoughts on these kind of MVC challenges?

mitchellsimoens
3 Nov 2012, 4:51 AM
Often times I use utility singleton classes to handle logic that doesn't really easily go into the controller but should be out of the view/store.