Class Factories

Parent Previous Next

Everytime you have a property with the suffix "renderer", that usually means it is of type "ClassFactory". What this means is that the property itself points to an object that exposes a newInstance() method. This method is responsible for generating an "Instance" of the class. These are usually renderer instances. So for example, the following properties on the grid (or the level or column) are of type ClassFactory:






In XML, you provide class factory values by using a fully qualified class name. For example:



   '                                        <column headerText="Editable Name" dataField="legalName" '+

   '                                           filterControl="TextInput" filterOperation="BeginsWith" paddingLeft="5" paddingBottom="5"  '+

   '                                           paddingRight="8" enableCellClickRowSelect="false" itemRenderer="myCompanyNameSpace.ItemRenderers_TextInputRenderer">'+

   '                                        </column>'+

However, in API, you could do the same thing in the following manner (this is what the XML builder does internally anyway):

col.itemRenderer=(new flexiciousNmsp.ClassFactory(myCompanyNameSapce.ItemRenderers_TextInputRenderer));


Although you may not have to directory work with the ClassFactory class, below is the code for it. It is a very simple class responsible for generating the instances of renderers.


/**

* Flexicious

* Copyright 2011, Flexicious LLC

*/

(function () {

   "use strict";

   var ClassFactory;

   /**

    * A generator class that instantiates new classes of type classConstruct.

    * @constructor

    * @class ClassFactory

    * @namespace flexiciousNmsp

    * @extends TypedObject

    * @param classConstruct

    * @param [props]

    * @param [passPropertiesToConstructor]

    *

    */

   ClassFactory = function (classConstruct,props,passPropertiesToConstructor) {

       flexiciousNmsp.TypedObject.apply(this);

       /**

        * The constructor to instantiate

        * @type Function

        */

       this.classConstruct = classConstruct;

       /**

        * The properties to apply to this constructors

        */

       this.properties = props;

       /**

        * If true, passes the properties to constructor, if false, loops through properties and

        * sets them individually on the bean

        */

       this.passPropertiesToConstructor=passPropertiesToConstructor;


   };

   flexiciousNmsp.ClassFactory = ClassFactory; //add to name space

   ClassFactory.prototype = new flexiciousNmsp.TypedObject(); //setup hierarchy

   ClassFactory.prototype.typeName = ClassFactory.typeName = "ClassFactory";//for quick inspection

   ClassFactory.prototype.getClassNames = function () { //for support of "is" keyword

       return ["TypedObject", this.typeName];

   };

   /**

    * Creates a new instance of the object specified by the class construct

    * @return {*}

    */

   ClassFactory.prototype.newInstance=function(){

       var obj;


       if(this.passPropertiesToConstructor){

           obj = new  this.classConstruct(this.properties);

       }else{

           obj = new  this.classConstruct();

           if(this.properties ){

               for (var prop in this.properties){

                   obj[prop] = this.properties[prop];

               }

           }


       }

       return obj;

   };


}());