Instantiation of a Widget, simplest way

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Instantiation of a Widget, simplest way

lazaridis_com
I've seen some examples, an instantiate a tree like in the code below. This works fine.

Is there any way to reduce the 3 lines of code (which is marked bold)?

            dojo.addOnLoad(function() {
                           
                var tree1 = new dijit.Tree({
                    model: model,
                    persist:"false",
                    showRoot: false,
                    dndController: "dijit.tree.dndSource",
                    dragThreshold: 8,
                    checkAcceptance: function(source,nodes) {
                        return true;
                    },
                    checkItemAcceptance: function (node,source) {
                        return true;
                    },
                }, document.createElement('div'));
               
                dojo.byId("tree1Div").appendChild(tree1.domNode);

                tree1.startup();

            });
        </script>



    </head>
   
    <body class=" claro ">
        <div id="tree1Div">
        </div>
    </body>

</html>
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

Sébastien Le Ray
Le Tue, 26 Apr 2011 15:16:15 -0700 (PDT),
lazaridis_com <[hidden email]> a écrit :

> I've seen some examples, an instantiate a tree like in the code
> below. This works fine.
>
> Is there any way to reduce the 3 lines of code (which is marked bold)?

Well, your mail is text only so bold vanished :-)

>
>             dojo.addOnLoad(function() {
>                            
>                 var tree1 = new dijit.Tree({
>                     model: model,
>                     persist:"false",
>                     showRoot: false,
>                     dndController: "dijit.tree.dndSource",
>                     dragThreshold: 8,
>                     checkAcceptance: function(source,nodes) {
>                         return true;
>                     },
>                     checkItemAcceptance: function (node,source) {
>                         return true;
>                     },
>                 }, document.createElement('div'));
>                
>                 dojo.byId("tree1Div").appendChild(tree1.domNode);
I guess you could directly do tree = new dijit.Tree({ ...},
dojo.byId("tree1Div")); tree.startup(); to avoid appending the DOMNode…
At least this can be done for most dijits. Since you do not use any
default values for init parameters you cannot simplify…

>
>                 tree1.startup();
>             });
>         </script>
>
>
>
>     </head>
>    
>     <body class=" claro ">
>         <div id="tree1Div">
>         </div>
>     </body>
>
> </html>
>
> --
> View this message in context:
> http://dojo-toolkit.33424.n3.nabble.com/Instantiation-of-a-Widget-simplest-way-tp2867781p2867781.html
> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
> _______________________________________________ FAQ:
> http://dojotoolkit.org/support/faq Book: http://docs.dojocampus.org
> [hidden email]
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest

_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

Bill Keese-2
Well, if tree1Div is the container for the tree (rather than a placeholder node to be replaced by the Tree):  new dijit.Tree({...}).placeAt("tree1Div")

On Wed, Apr 27, 2011 at 3:17 PM, Sébastien Le Ray <[hidden email]> wrote:
Le Tue, 26 Apr 2011 15:16:15 -0700 (PDT),
lazaridis_com <[hidden email]> a écrit :

> I've seen some examples, an instantiate a tree like in the code
> below. This works fine.
>
> Is there any way to reduce the 3 lines of code (which is marked bold)?

Well, your mail is text only so bold vanished :-)

>
>             dojo.addOnLoad(function() {
>
>                 var tree1 = new dijit.Tree({
>                     model: model,
>                     persist:"false",
>                     showRoot: false,
>                     dndController: "dijit.tree.dndSource",
>                     dragThreshold: 8,
>                     checkAcceptance: function(source,nodes) {
>                         return true;
>                     },
>                     checkItemAcceptance: function (node,source) {
>                         return true;
>                     },
>                 }, document.createElement('div'));
>
>                 dojo.byId("tree1Div").appendChild(tree1.domNode);

I guess you could directly do tree = new dijit.Tree({ ...},
dojo.byId("tree1Div")); tree.startup(); to avoid appending the DOMNode…
At least this can be done for most dijits. Since you do not use any
default values for init parameters you cannot simplify…

>
>                 tree1.startup();
>             });
>         </script>
>
>
>
>     </head>
>
>     <body class=" claro ">
>         <div id="tree1Div">
>         </div>
>     </body>
>
> </html>
>
> --
> View this message in context:
> http://dojo-toolkit.33424.n3.nabble.com/Instantiation-of-a-Widget-simplest-way-tp2867781p2867781.html
> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
> _______________________________________________ FAQ:
> http://dojotoolkit.org/support/faq Book: http://docs.dojocampus.org
> [hidden email]
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest



_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

lazaridis_com
Bill Keese-2 wrote
Well, if tree1Div is the container for the tree (rather than a placeholder
node to be replaced by the Tree):  new dijit.Tree({...}).placeAt("tree1Div")
simple enough.

What about this "tree1.startup();". If I remove it, it still works.

When it becomes necessary to call it?

I tried to "get it" here, but I failed:

http://dojotoolkit.org/api/1.6/dijit/Tree


Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

Bill Keese-2
Technically you are required to call startup() on every widget, but some widgets work without it, IIRC Tree is one of those.

On Wed, Apr 27, 2011 at 11:48 PM, lazaridis_com <[hidden email]> wrote:

Bill Keese-2 wrote:
>
> Well, if tree1Div is the container for the tree (rather than a placeholder
> node to be replaced by the Tree):  new
> dijit.Tree({...}).placeAt("tree1Div")
>

simple enough.

What about this "tree1.startup();". If I remove it, it still works.

When it becomes necessary to call it?

I tried to "get it" here, but I failed:

http://dojotoolkit.org/api/1.6/dijit/Tree




--
View this message in context: http://dojo-toolkit.33424.n3.nabble.com/Instantiation-of-a-Widget-simplest-way-tp2867781p2870599.html
Sent from the Dojo Toolkit mailing list archive at Nabble.com.
_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

lazaridis_com
Bill Keese-2 wrote
On Wed, Apr 27, 2011 at 11:48 PM, lazaridis_com <[hidden email]> wrote:
>
> Bill Keese-2 wrote:
> >
> > Well, if tree1Div is the container for the tree (rather than a
> placeholder
> > node to be replaced by the Tree):  new
> > dijit.Tree({...}).placeAt("tree1Div")
> >
>
> simple enough.
>
> What about this "tree1.startup();". If I remove it, it still works.
>
> When it becomes necessary to call it?
>
> I tried to "get it" here, but I failed:
>
> http://dojotoolkit.org/api/1.6/dijit/Tree

Technically you are required to call startup() on every widget, but some
widgets work without it, IIRC Tree is one of those.
Thus the shortest technically valid method would be

//without an instance-variable, as startup does not return the object)
dijit.Tree({...}).placeAt("tree1Div").startup();

//with an instance variable
new tree1 = dijit.Tree({...});
tree1.placeAt("tree1Div").startup();

Any way to get to an "oneliner" here, without having to overload the "startup" function?
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

neonstalwart
one line:

(var tree1 = new dijit.Tree({...}).placeAt('tree1Div')).startup();

given that containers will call startup on their children when their
startup is called, you can get away without calling startup for many
cases and simply call startup once on your "root" layout element after
adding all the descendants.

ben...

On 4/28/2011 11:24 AM, lazaridis_com wrote:

> Bill Keese-2 wrote:
>> On Wed, Apr 27, 2011 at 11:48 PM, lazaridis_com
>> &lt;[hidden email]&gt; wrote:
>>> Bill Keese-2 wrote:
>>>> Well, if tree1Div is the container for the tree (rather than a
>>> placeholder
>>>> node to be replaced by the Tree):  new
>>>> dijit.Tree({...}).placeAt("tree1Div")
>>>>
>>> simple enough.
>>>
>>> What about this "tree1.startup();". If I remove it, it still works.
>>>
>>> When it becomes necessary to call it?
>>>
>>> I tried to "get it" here, but I failed:
>>>
>>> http://dojotoolkit.org/api/1.6/dijit/Tree
>> Technically you are required to call startup() on every widget, but some
>> widgets work without it, IIRC Tree is one of those.
>>
>>
> Thus the shortest technically valid method would be
>
> //without an instance-variable, as startup does not return the object)
> dijit.Tree({...}).placeAt("tree1Div").startup();
>
> //with an instance variable
> new tree1 = dijit.Tree({...});
> tree1.placeAt("tree1Div").startup();
>
> Any way to get to an "oneliner" here, without having to overload the
> "startup" function?
>
> --
> View this message in context: http://dojo-toolkit.33424.n3.nabble.com/Instantiation-of-a-Widget-simplest-way-tp2867781p2875113.html
> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
> _______________________________________________
> FAQ: http://dojotoolkit.org/support/faq
> Book: http://docs.dojocampus.org
> [hidden email]
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

lazaridis_com
2011/4/28 neonstalwart [via Dojo Toolkit] <[hidden email]>

> Thus the shortest technically valid method would be

>
> //without an instance-variable, as startup does not return the object)
> dijit.Tree({...}).placeAt("tree1Div").startup();
>
> //with an instance variable
> new tree1 = dijit.Tree({...});
> tree1.placeAt("tree1Div").startup();
>
> Any way to get to an "oneliner" here, without having to overload the
> "startup" function?
>
one line:

(var tree1 = new dijit.Tree({...}).placeAt('tree1Div')).startup();

This seems to fail, but ok, it's anyway to "ugly".

The 2 liner is fine for now.
given that containers will call startup on their children when their
startup is called, you can get away without calling startup for many
cases and simply call startup once on your "root" layout element after
adding all the descendants.

I understand, this sounds easy.

Curious: Is there a way to declare the "<body>" as a "dummy" Container, thus it get's a startup (which would call child startups)?
 



Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

neonstalwart

>
> Curious: Is there a way to declare the "<body>" as a "dummy"
> Container, thus it get's a startup (which would call child startups)?
>
a common practice might be to put a BorderContainer or StackContainer
(depending on your needs) as the "root" of your layout - programmatically:
var root = new dijit.layout.BorderContainer({...}).placeAt(dojo.body());
// gives us 5 regions to place other widgets
// continue building your app eg.
var mainStack = new dijit.layout.StackContainer({ region: 'center'
}).placeAt(root); // put a stack in the center region
var page1 = new your.Page1({...}).placeAt(mainStack); // add a page to
the stack
...

root.startup();  // triggers startup for mainStack -> triggers startup
for page1 -> ...

i've never tried but i would think that if you try to build a widget
that consumes the body as it's domNode you'd probably break something:
var root = new dijit.layout.BorderContainer({...}, dojo.body()); //
probably breaks something ???

also, just to clarify, it helps to have it clear in your mind the
difference between DOM nodes and widgets.  we don't add any functions to
DOM nodes - so we don't provide a way to give <body> a startup.  the
widget has the startup function.  a widget has a domNode property and
that is the link from widget -> DOM but be sure you have it clear that a
widget !== DOM node.

ben...
_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

lazaridis_com


2011/4/29 neonstalwart[via Dojo Toolkit] <[hidden email]>

>
> Curious: Is there a way to declare the "<body>" as a "dummy"
> Container, thus it get's a startup (which would call child startups)?
>
a common practice might be to put a BorderContainer or StackContainer
(depending on your needs) as the "root" of your layout - programmatically:
[...]
 
var root = new dijit.layout.BorderContainer({...}, dojo.body()); //
probably breaks something ???

ok
 
[...] we don't add any functions to
DOM nodes - so we don't provide a way to give <body> a startup. [...]

ok

The last one:

Is there any existent central function I can call to "play safe & without thought", like


dijit.autoStartup()   // scans automated for existent startup() functions and calls them.


Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

Bill Keese-2
> Is there any existent central function I can call to "play safe & without thought", like
> dijit.autoStartup()   // scans automated for existent startup() functions and calls them.

No, but the parser does that automatically for you.   If you aren't using the parser, you can do:

dojo.forEach(dijit.findWidgets(dojo.body()), function(widget){
   if(widget.startup) widget.startup();
});

On Sat, Apr 30, 2011 at 2:46 PM, lazaridis_com <[hidden email]> wrote:


2011/4/29 neonstalwart[via Dojo Toolkit] <[hidden email]>


>
> Curious: Is there a way to declare the "<body>" as a "dummy"
> Container, thus it get's a startup (which would call child startups)?
>
a common practice might be to put a BorderContainer or StackContainer
(depending on your needs) as the "root" of your layout - programmatically:
[...]
 
var root = new dijit.layout.BorderContainer({...}, dojo.body()); //
probably breaks something ???

ok
 
[...] we don't add any functions to
DOM nodes - so we don't provide a way to give <body> a startup. [...]

ok

The last one:

Is there any existent central function I can call to "play safe & without thought", like


dijit.autoStartup()   // scans automated for existent startup() functions and calls them.





View this message in context: Re: Instantiation of a Widget, simplest way
Sent from the Dojo Toolkit mailing list archive at Nabble.com.

_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest



_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

lazaridis_com


2011/4/30 Bill Keese-2[via Dojo Toolkit] <[hidden email]>

On Sat, Apr 30, 2011 at 2:46 PM, lazaridis_com <[hidden email]> wrote:
[...]
Is there any existent central function I can call to "play safe & without thought", like

dijit.autoStartup()   // scans automated for existent startup() functions and calls them.

No, but the parser does that automatically for you.  
[...]
 

Thus having the following:

<script type="text/javascript" src="dojo/dojo.js"
        djConfig="parseOnLoad: true">
</script>

would ensure that .startup is called on every widget, even if they were instantiated by code?
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

Bill Keese-2
No, I just meant that the parser calls startup on widgets instantiated declaratively, using data-dojo-type.

On Mon, May 2, 2011 at 4:27 AM, lazaridis_com <[hidden email]> wrote:


2011/4/30 Bill Keese-2[via Dojo Toolkit] <[hidden email]>

On Sat, Apr 30, 2011 at 2:46 PM, lazaridis_com <[hidden email]> wrote:
[...]
Is there any existent central function I can call to "play safe & without thought", like

dijit.autoStartup()   // scans automated for existent startup() functions and calls them.

No, but the parser does that automatically for you.  
[...]
 

Thus having the following:

<script type="text/javascript" src="dojo/dojo.js"
        djConfig="parseOnLoad: true">
</script>

would ensure that .startup is called on every widget, even if they were instantiated by code?



View this message in context: Re: Instantiation of a Widget, simplest way
Sent from the Dojo Toolkit mailing list archive at Nabble.com.

_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest



_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

lazaridis_com


2011/5/2 Bill Keese-2[via Dojo Toolkit] <[hidden email]>
On Mon, May 2, 2011 at 4:27 AM, lazaridis_com <[hidden email]> wrote:
2011/4/30 Bill Keese-2[via Dojo Toolkit] <[hidden email]>
On Sat, Apr 30, 2011 at 2:46 PM, lazaridis_com <[hidden email]> wrote:
[...]
Is there any existent central function I can call to "play safe & without thought", like

dijit.autoStartup()   // scans automated for existent startup() functions and calls them.

No, but the parser does that automatically for you.  
[...]
 

Thus having the following:

<script type="text/javascript" src="dojo/dojo.js"
        djConfig="parseOnLoad: true">
</script>

would ensure that .startup is called on every widget, even if they were instantiated by code?
No, I just meant that the parser calls startup on widgets instantiated declaratively, using data-dojo-type.

ok, my use case here is "code instantiated widgets" (will take a look a declarative instantiation later).

But as you've answered, I take it into the overview:

* declarative instantiation, use "djConfig="parseOnLoad: true", and it's save.

* programmatic instantiation,

no function available, thus call startup manually, or:

a) call to .startup() of a container calls all the child's .startup().

b) implement a function that detects the root widgets and calls startup() on them (containers call children's startup)

c) implement a function that detects all widgets and calls startup() on them.

d) implement option: djConfig="startupOnLoad: true|false" (with default: false)


Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

Bill Keese-2
Right.    Remember though that startup() runs recursively so you only want to call startup() on the top level widgets.   (Calling startup() on all widgets would probably work too, but would be wasteful.)

On Mon, May 2, 2011 at 12:21 PM, lazaridis_com <[hidden email]> wrote:


2011/5/2 Bill Keese-2[via Dojo Toolkit] <[hidden email]>

On Mon, May 2, 2011 at 4:27 AM, lazaridis_com <[hidden email]> wrote:
2011/4/30 Bill Keese-2[via Dojo Toolkit] <[hidden email]>
On Sat, Apr 30, 2011 at 2:46 PM, lazaridis_com <[hidden email]> wrote:
[...]
Is there any existent central function I can call to "play safe & without thought", like

dijit.autoStartup()   // scans automated for existent startup() functions and calls them.

No, but the parser does that automatically for you.  
[...]
 

Thus having the following:

<script type="text/javascript" src="dojo/dojo.js"
        djConfig="parseOnLoad: true">
</script>

would ensure that .startup is called on every widget, even if they were instantiated by code?
No, I just meant that the parser calls startup on widgets instantiated declaratively, using data-dojo-type.

ok, my use case here is "code instantiated widgets" (will take a look a declarative instantiation later).

But as you've answered, I take it into the overview:

* declarative instantiation, use "djConfig="parseOnLoad: true", and it's save.

* programmatic instantiation,

no function available, thus call startup manually, or:

a) call to .startup() of a container calls all the child's .startup().

b) implement a function that detects the root widgets and calls startup() on them (containers call children's startup)

c) implement a function that detects all widgets and calls startup() on them.

d) implement option: djConfig="startupOnLoad: true|false" (with default: false)





View this message in context: Re: Instantiation of a Widget, simplest way
Sent from the Dojo Toolkit mailing list archive at Nabble.com.

_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest



_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

lazaridis_com
2011/5/2 Bill Keese-2 [via Dojo Toolkit] <[hidden email]>
On Mon, May 2, 2011 at 12:21 PM, lazaridis_com <[hidden email]> wrote:
[...]
Thus having the following:

<script type="text/javascript" src="dojo/dojo.js"
        djConfig="parseOnLoad: true">
</script>

would ensure that .startup is called on every widget, even if they were instantiated by code?
No, I just meant that the parser calls startup on widgets instantiated declaratively, using data-dojo-type.


ok, my use case here is "code instantiated widgets" (will take a look a declarative instantiation later).

But as you've answered, I take it into the overview:

* declarative instantiation, use "djConfig="parseOnLoad: true", and it's save.

* programmatic instantiation,

no function available, thus call startup manually, or:

a) call to .startup() of a container calls all the child's .startup().

b) implement a function that detects the root widgets and calls startup() on them (containers call children's startup)

c) implement a function that detects all widgets and calls startup() on them.

d) implement option: djConfig="startupOnLoad: true|false" (with default: false)
Right.    Remember though that startup() runs recursively so you only want to call startup() on the top level widgets.   (Calling startup() on all widgets would probably work too, but would be wasteful.)

Of course. To make the list more clear, ordered-by-elegance and commented:

a) implement a function that detects all widgets and calls startup() on them - (not very elegant and wasteful).

b) call to .startup() of a container calls all the child's .startup(). - (elegant, but not general valid and "newcomer-save")

c) implement a function that detects the root widgets and calls startup() on them (containers call children's startup) - (elegant and newcomer-save)

d) implement option: djConfig="startupOnLoad: true|false" (with default: false) - (most elegant way, save for newcomers and for  tired-experts-overworked-at-night).


Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

neonstalwart

>
> Of course. To make the list more clear, ordered-by-elegance and commented:
>
> a) implement a function that detects all widgets and calls startup()
> on them - (not very elegant and wasteful).
>
> b) call to .startup() of a container calls all the child's .startup().
> - (elegant, but not general valid and "newcomer-save")
why is this not generally valid or newcomer-safe?  and if you're
particularly interested in being newcomer-safe, declarative
instantiation with the parser is the safest/simplest option.

>
> c) implement a function that detects the root widgets and calls
> startup() on them (containers call children's startup) - (elegant and
> newcomer-save)
>
> d) implement option: djConfig="startupOnLoad: true|false" (with
> default: false) - (most elegant way, save for newcomers and for  
> tired-experts-overworked-at-night).
i don't see how all of these options are valid (particularly 'd' and
maybe 'c').  how do you propose to detect when a widget is ready to have
startup called?  you need to know when it is attached to the document
before you can call startup and there is no way of detecting this
automatically.  this is why calling startup is a manual process for
programatically instantiated widgets.  for declarative widgets, the
parser has knowledge of when it added the widget to the document and can
call startup at the appropriate time.  i believe that it would be
difficult to improve the current process for programatically
instantiating widgets.  imo one improvement would be for startup to
return the widget so that you could do
   var w = new Widget().placeAt(node).startup();
apart from that, is there something else you think that could be improved?

also, if you follow my advice and set off a chain of startup calls, what
i didn't mention is that with a large enough tree, this can cause a
noticeable delay for the end user.  a developer with some insight to the
specific use case can potentially improve the user experience by making
decisions about when to call startup on particular pieces and therefore
reducing the noticeable effect of these delays.

ben...
_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://docs.dojocampus.org
[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: Instantiation of a Widget, simplest way

lazaridis_com
2011/5/3 neonstalwart [via Dojo Toolkit] <[hidden email]>

> Of course. To make the list more clear, ordered-by-elegance and commented:
>
> a) implement a function that detects all widgets and calls startup()
> on them - (not very elegant and wasteful).
>
> b) call to .startup() of a container calls all the child's .startup().
> - (elegant, but not general valid and "newcomer-save")
why is this not generally valid or newcomer-safe? 

because there is not always a container, and because the newcomer has to know/think/remind
 
and if you're
particularly interested in being newcomer-safe, declarative
instantiation with the parser is the safest/simplest option.

I have understood the simplicity of the parser, but the use-case here is the programmatic instantiation / placement.
 
> c) implement a function that detects the root widgets and calls
> startup() on them (containers call children's startup) - (elegant and
> newcomer-save)
>
> d) implement option: djConfig="startupOnLoad: true|false" (with
> default: false) - (most elegant way, save for newcomers and for  
> tired-experts-overworked-at-night).
i don't see how all of these options are valid (particularly 'd' and
maybe 'c').  how do you propose to detect when a widget is ready to have
startup called?
[...]

Note that I must reduce my involvement at this point. I've filed a ticket subjecting startupAt(), this should make things simpler and saver, without any automation's:

http://bugs.dojotoolkit.org/ticket/12846

I've understood your other writings, and I'll answer "by example" (please do not "pick" on invalid details, it's a general construct subjecting frameworks):

dojoPlaySave="true" // alternate naming: dojoAutoInit
Note: if you use this option, many things are done automatically, even if you don't use the parser and instantiate e.g. your widgets manually. In small projects, there's not much overhead. If you have a more complex application then "dojoPlaySave" _can_ lead to delays in execution or sometimes to unexpected results. In this case, you have to check the "dojo.PlaySave.js" thus you know what you have to to when deactivation dojoPlaySave. At the moment you write an complex application, you'll be more familiar with the toolkit, thus handling the initializations "by hand" should be very easy.

// within dojo.PlaySave.js
// autoStartup: whenever a widget is attached to the DOM, it's "startup" function is called.
// This is achieved by the handler xyz, which fires onDomWidgetAttached. Speed overhead is minimal
// [...] ( further automations)