Results 1 to 4 of 4

Thread: common util methods in MVC

  1. #1
    Ext JS Premium Member
    Join Date
    Sep 2010
    Posts
    51

    Default common util methods in MVC

    In MVC application where we need to write common util methods which can be accessed by controllers and views?

    Thanks,
    At
    AS

  2. #2
    Sencha User friend's Avatar
    Join Date
    Apr 2011
    Posts
    895
    Answers
    106

    Default

    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.

  3. #3
    Ext JS Premium Member
    Join Date
    Jan 2008
    Location
    Germany, Berlin
    Posts
    135
    Answers
    21

    Default

    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?

    Code:
    Ext.define('MyApp.util.Renderers', {
      extend: ???
    
      singleton: true,
    
      renderThis: function () {
        //doSomething
      },
    
      renderThat: function () {
        //doSomeOtherThing
      }
    });

  4. #4
    Sencha User friend's Avatar
    Join Date
    Apr 2011
    Posts
    895
    Answers
    106

    Default

    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:

    Code:
    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;
      }
     
    });

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •