creating another component in the component's initialize method

Last post 10-16-2007 10:59 PM by null12345. 4 replies.

Sort Posts:

  • creating another component in the component's initialize method

    10-16-2007, 1:08 PM
    • Member
      12 point Member
    • null12345
    • Member since 07-21-2007, 12:35 PM
    • Posts 38

    I'm trying to create another component in the component's initialize method.

    When I create the component in the pageLoad stage, it works fine. But If I create the component in the Init stage, the second component's initialize method doesn't get callled.

    Here's the codes.

     

     

    Type.registerNamespace('Samples');

    Samples.MyComponent1 = function() {
    Samples.MyComponent1.initializeBase(this);
    }

    Samples.MyComponent1.prototype = {
    initialize : function() {
    Samples.MyComponent1.callBaseMethod(this, 'initialize');

    $create(Samples.MyComponent2, {}, {}, {});

    alert(Object.getTypeName(this) + " get initialized!");
    },

    dispose : function() {
    alert(Object.getTypeName(this) + " get disposed!");
    Samples.MyComponent1.callBaseMethod(this, 'dispose');
    }
    }

    Samples.MyComponent1.registerClass('Samples.MyComponent1', Sys.Component);

    Samples.MyComponent2 = function() {
    Samples.MyComponent2.initializeBase(this);
    }

    Samples.MyComponent2.prototype = {
    initialize : function() {
    Samples.MyComponent2.callBaseMethod(this, 'initialize');
    alert(Object.getTypeName(this) + " get initialized!");
    },

    dispose : function() {
    alert(Object.getTypeName(this) + " get disposed!");
    Samples.MyComponent2.callBaseMethod(this, 'dispose');
    }
    }

    Samples.MyComponent2.registerClass('Samples.MyComponent2', Sys.Component);

    if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
     
     

      

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" >
    <Scripts>
    <asp:ScriptReference Path="~/scripts/Test.js" />
    </Scripts>
    </asp:ScriptManager>

    <script type="text/javascript">

    Sys.Application.add_init(pageInit);

    function pageInit() {
    $create(Samples.MyComponent1, {}, {}, {}, null);
    }

    function pageLoad() {
    }

    </script>
    </div>
    </form>
    </body>
    </html>
     

     

  • Re: creating another component in the component's initialize method

    10-16-2007, 3:44 PM
    Answer
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello.

    this is something that i have never done. generally, i'd create the control outside and pass it as reference to the top control (but again, you might have a requirement which makes this impossible). if you look at the Sys$_Application$endCreateComponents you'll see why you're not getting the call. here's the code for that method:

    function Sys$_Application$endCreateComponents() {
        var components = this._secondPassComponents;
        for (var i = 0, l = components.length; i < l; i++) {
            var component = components[i].component;
            Sys$Component$_setReferences(component, components[i].references);
            component.endUpdate();
        }
        this._secondPassComponents = [];
        this._creatingComponents = false;
    }

    when you get the initialization part of the for, components' length is set to 1 since you only have one component. during the 1st step of the cycle, you'll get a new component added to that collection. unfortunately, the above code will always be false and the initialize of the second component won't be called.

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: creating another component in the component's initialize method

    10-16-2007, 5:05 PM
    Answer
    • Star
      14,200 point Star
    • bleroy
    • Member since 04-12-2003, 7:09 AM
    • Redmond
    • Posts 2,296

    Luis is right, $create is only intended as a shortcut for page developers and as something that server-side components use to instantiate themselves from in-page code. Component developers writing client-side code should just new up the component and initialize it "by hand".

    $create has this two pass model that hides the complexities of creating components that refer to each other. For code inside a component, there is no need for such a model.

    Also, in production code, you'd assign the created component to a member field so that you can properly dispose of it from the dispose of your primary component.

    Bertrand
    ----
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: creating another component in the component's initialize method

    10-16-2007, 6:10 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello again.

    btw, i've just written a small post detailing the problem and how to solve it:

    http://msmvps.com/blogs/luisabreu/archive/2007/10/16/you-shouldn-t-call-create-from-within-a-component-to-create-another-component.aspx

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: creating another component in the component's initialize method

    10-16-2007, 10:59 PM
    • Member
      12 point Member
    • null12345
    • Member since 07-21-2007, 12:35 PM
    • Posts 38

    Thanks a lot for the detailed explanation!

     

Page 1 of 1 (5 items)