Load the default view using "RESTful" URLs

17 views
Skip to first unread message

Amine Salmen Rekik

unread,
Nov 18, 2014, 5:05:03 AM11/18/14
to nagare...@googlegroups.com
Hello,

In the RestfulUrl tutorial, we learned that we can initialize a component  using the URL and the@presentation.init_for.  decorator.

In my application, I need to initialize a component if the URL is not defined, So I used the function  @presentation.init_for(App, 'not url') in order to handle request without URL, but it doesn't work!

In the attachment file, I defined an application App composed of a menu and a content, when the user clicks an item in the menu, it will be rendered in the content block. My aim is to display the first item if the user doesn't specify its name in the URL. (I know that I can init the first menu in the __init__ function of the App, but I need to set it in the @presentation.init_for function)

Can you help me?

 
restFull.py

Alain Poirier

unread,
Nov 18, 2014, 7:31:10 AM11/18/14
to nagare...@googlegroups.com
Hi Amine,

> Le 18 nov. 2014 à 11:05, Amine Salmen Rekik <amine...@gmail.com> a écrit :
>
> Hello,
>
> In the RestfulUrl tutorial, we learned that we can initialize a component using the URL and t...@presentation.init_for. decorator.
>
> In my application, I need to initialize a component if the URL is not defined, So I used the function @presentation.init_for(App, 'not url') in order to handle request without URL, but it doesn't work!
>
> In the attachment file, I defined an application App composed of a menu and a content, when the user clicks an item in the menu, it will be rendered in the content block. My aim is to display the first item if the user doesn't specify its name in the URL.


> (I know that I can init the first menu in the __init__ function of the App, but I need to set it in the @presentation.init_for function)

Yet this is exactly what we do. For an empty URL, the App component is initialized
by its ‘__init__()’ method. Normal behavior.

In your example that means simply adding line #9:

self.selectMenu(self.menuItems[0])

> Can you help me?

Why in your case aren’t you able to that? Why to you abolutely need to initialized
it in a @presentation.init_for function?

Best regards,
Alain

Amine Salmen Rekik

unread,
Nov 18, 2014, 8:22:47 AM11/18/14
to nagare...@googlegroups.com
Hi :)
Thank you for you replay


Why in your case aren’t you able to that? Why to you abolutely need to initialized
it in a @presentation.init_for function?

I don't like to initialize the component in the init function because when the function selectMenu is invoked, my application loads the data from the database.

So when we set the menu name in the URL, the selectMenu function is invoked twice ( the first one in the _init_ function and the second one in @presentation.init_for function)  . As a consequence, we will have 2 requests to the database and we will load unused data.

My idea is to invoke the selectMenu (request data) in the @presentation.init_for function: If the menu name is specified, the application loads the appropriate data, otherwise we load the data of the first menu.

Best regards,
Amine
 
Best regards,
Alain

Alain Poirier

unread,
Nov 19, 2014, 6:50:28 AM11/19/14
to nagare...@googlegroups.com
Le 18 nov. 2014 à 14:22, Amine Salmen Rekik <amine...@gmail.com> a écrit :
>
> Hi :)
> Thank you for you replay
>
>> Why in your case aren’t you able to that? Why to you abolutely need to initialized
>> it in a @presentation.init_for function?
>
> I don't like to initialize the component in the init function because when the function selectMenu is invoked, my application loads the data from the database.
>
> So when we set the menu name in the URL, the selectMenu function is invoked twice ( the first one in the _init_ function and the second one in @presentation.init_for function) . As a consequence, we will have 2 requests to the database and we will load unused data.

Then, to me it looks like you want to defer the initialization of your objects on
their first access. In your example that can simply be done by using a ‘content’
property (full modifications in the file in attachment):

@property
def content(self):
if not self._content():
self._content.becomes(self.menuItems[0])
return self._content
restFull.py

Amine Salmen Rekik

unread,
Dec 1, 2014, 2:25:34 AM12/1/14
to nagare...@googlegroups.com
Hello,

Thank you :) I will use the property decoration :)

Cdt,
Reply all
Reply to author
Forward
0 new messages