Adding Before & After Filters in Sencha Touch MVC Controllers

BeforeFilters in RoR are a great way to manage controller accesses or do execute any other specific controller action triggers.

Here is a simple way of mimicking this great RoR feature on Sencha Touch MVC.

Create a file named `app/controllers/Controllers.js` with the following content:

?View Code JAVASCRIPT
Ext.Dispatcher.on('before-dispatch', function(interaction) {
  if(Ext.isFunction(interaction.controller.beforeFilter)) {
    return interaction.controller.beforeFilter.call();
  };
  return true;
});
 
 
Ext.Dispatcher.on('dispatch', function(interaction) {
  if(Ext.isFunction(interaction.controller.afterFilter)) {
    return interaction.controller.afterFilter.call();
  };
  return true;
});

Include this in your HTML document.

Now, you can add your beforeFilter action to all your controllers like following:

?View Code JAVASCRIPT
Ext.regController('TestController', {
  beforeFilter: function() {
    console.log('Hi! I am a beforeFilter!');
  },
  afterFilter: function() {
    console.log('Hi! I am an afterFilter!');
  },
  index: function() {
    App.views.viewport.reveal('testView');
  }
});

Reference: Adding Callbacks to your Sencha Touch Controllers

This entry was posted in ExtJS, Javascript, Sencha Touch and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*