10 Replies Latest reply on Sep 5, 2012 6:02 AM by mediablur

    Custom DataGrid Item Renderers failing in loaded module.

    Malkyne

      I'm making an application that manages a bunch of types of data records.  You can load plug-ins (in the form of swf modules) that provide additional record-models, and views and editors for those record types.

       

      Mostly, it works great, except for one serious problem:  Regardless of where they are defined (inline, in the same file, in the same swf, or in the interface swc), any custom spark grid item renderers that are used in the loaded views or editors cause my application to crash at runtime.  Weirdly, my DataGroup item renderers don't seem to have the same problem, so far.

       

      I have found a workaround.  That is, if I attach the renderer at runtime, at creationComplete, and make a reference to that renderer in the main application, then it works fine.  However, this is a bit of a pain, if one of my plugin writers wishes to use a custom grid item renderer that I didn't anticipate, in advance.  I would really like a way for my plugin-developers to be able to define new renderers, if necessary.  Any clues on how to tackle this?

       

      Here is the stack trace from the crash:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.

      at spark.components::Group/addElement()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\com ponents\Group.as:1342]

      at spark.components.gridClasses::GridLayout/createTypicalItemRenderer()[E:\dev\4.5.1\framewo rks\projects\spark\src\spark\components\gridClasses\GridLayout.as:748]

      at spark.components.gridClasses::GridLayout/updateTypicalCellSizes()[E:\dev\4.5.1\frameworks \projects\spark\src\spark\components\gridClasses\GridLayout.as:883]

      at spark.components.gridClasses::GridLayout/measure()[E:\dev\4.5.1\frameworks\projects\spark \src\spark\components\gridClasses\GridLayout.as:444]

      at spark.components.supportClasses::GroupBase/measure()[E:\dev\4.5.1\frameworks\projects\spa rk\src\spark\components\supportClasses\GroupBase.as:1148]

      at mx.core::UIComponent/measureSizes()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\cor e\UIComponent.as:8496]

      at mx.core::UIComponent/validateSize()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\cor e\UIComponent.as:8420]

      at spark.components::Group/validateSize()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\c omponents\Group.as:1012]

      at mx.managers::LayoutManager/validateSize()[E:\dev\4.5.1\frameworks\projects\framework\src\ mx\managers\LayoutManager.as:665]

      at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\frame work\src\mx\managers\LayoutManager.as:792]

      at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projec ts\framework\src\mx\managers\LayoutManager.as:1180]

        • 2. Re: Custom DataGrid Item Renderers failing in loaded module.
          Malkyne Level 1

          Even the most minimalist test case for this is going to be kind of non-trivial, but I'll see what I can whip up for you, later today.

          • 3. Re: Custom DataGrid Item Renderers failing in loaded module.
            Malkyne Level 1

            It took some doing, but I managed to cook up an example that reproduces the problem that I'm seeing.  If you download my zip file, it includes the three project files that you need to repro the problem.  Grab it from here.

             

            The zip file includes three project files:

            DemoMainApplication.fxp -- This is the main application, which loads the module.

            DemoPluginInterface.fxpl -- This just includes a minimalist interface class we can use to interface with our module front.

            DemoPluginModule.fxp -- This is an example plugin that is loaded by our application.  It implements the interface in DemoPluginInterface.  It includes two other classes which are referenced (and instantiated) by the module.

             

            The application displays two buttons.  As the text on the buttons explain, the first button displays a DataGrid without any custom item renderers in it, and the second button displays a DataGrid that uses a custom item renderer (which is defined in the same file).  The first button works, and the second one causes a crash.  Specifically, when the DataGrid is doing its typical item initialization, it fails to instantiate a renderer object from the factory.

             

            If I make my two example components into separate modules, generating separate swfs, then the DataGrid loads the custom item renderer for the second one just fine.  However, I was hoping for my plugin developers to be able to deliver a plugin as a single swf, instead of a mess of 20.

             

            Is this an application context problem?  If so, is there a good workaround you can suggest?

             

            Tess

            • 4. Re: Custom DataGrid Item Renderers failing in loaded module.
              Flex harUI Adobe Employee

              I ran out of time to dig into it, but it might be the AS module issue.  The

              main class for a module that contains styleable entities must be defined in

              MXML.

               

              -Alex

              • 5. Re: Custom DataGrid Item Renderers failing in loaded module.
                Malkyne Level 1

                I ran out of time to dig into it...

                 

                No worries.  I'm happy I'm even getting any kind of reply!

                 

                The main class for a module that contains styleable entities must be defined in MXML.
                I hadn't tried this yet, so I figured I'd give this a shot!  Unfortunately, it brought me no joy. 
                I created a NewPluginFace.MXML file to use, instead of PluginFace.as, and I pointed my loader to the NewPluginFace.swf, and the resulting behavior was completely identical.
                Tess

                • 6. Re: Custom DataGrid Item Renderers failing in loaded module.
                  Flex harUI Adobe Employee

                  I actually figured this out yesterday, but apparently forgot to reply.

                   

                  The problem you are having has to do with modulefactory.  Components from a

                  module need to have their moduleFactory set to the module.  This is done for

                  you if you use the create() method on the factory, or you can set it

                  afterwards if you want.

                   

                  -Alex

                  • 7. Re: Custom DataGrid Item Renderers failing in loaded module.
                    Malkyne Level 1

                    Thanks!  You're brilliant.  That was exactly the answer.

                     

                    In case it helps someone else, I'll show the code I changed from the minimal test that I posted.

                     

                    In DemoMainApplication.mxml, I changed moduleReady(...) to this:

                     


                    private function moduleReady( moduleEvent:ModuleEvent ):void
                    {
                        _plugin = moduleEvent.module.factory.create() as IPluginFace;
                        addElement(_plugin as UIComponent);
                    
                        _dataGrid1 = _plugin.getExample1Instance();
                        _dataGrid1.moduleFactory = moduleEvent.module.factory;
                        _dataGrid2 = _plugin.getExample2Instance();
                        _dataGrid2.moduleFactory = moduleEvent.module.factory;
                        
                        button1.enabled = true;
                        button2.enabled = true;
                    }

                     

                    And it works, now.

                     

                    I should be able to adapt this for my purposes, in my real application.  So happy.

                     

                    Tess

                    • 8. Re: Custom DataGrid Item Renderers failing in loaded module.
                      Flex harUI Adobe Employee

                      Yes, or getExample1Instance should be able to set the moduleFactory based on

                      its own moduleFactory value.

                      • 9. Re: Custom DataGrid Item Renderers failing in loaded module.
                        juliojfc Level 1

                        The same problem also shows up when running sub-applications, loaded via a SWFLoader.

                         

                        But the workaround I found is a bit simpler... all you have to do is set the datagrid.moduleFactory to NULL upon initialization of the wrapping component.

                         

                        Do not ask me why, but that fixed it for me.

                         

                        FYI, same issue also occurs with itemEditors.

                         

                        using Adobe's Flex SDK 4.6.

                        • 10. Re: Custom DataGrid Item Renderers failing in loaded module.
                          mediablur

                          The following also seems to work:

                           

                          <s:DataGrid id="myDataGrid"          

                                      creationComplete="myDataGrid.moduleFactory = null"

                                      [...] />