Avoid Custom widget "load" being called.

17 views
Skip to first unread message

Fotis Paraskevopoulos

unread,
Mar 10, 2015, 10:31:05 AM3/10/15
to apostr...@googlegroups.com
Hello Tom,

I have a created a simple custom widget which does not extend apostrophe-snippet.

Right before the widget is rendered on a page, I would like to customize the "item" a bit. It is my understanding that I can do this calling/overriding "load". I followed the feed-rss example.

However this "load" is being called for all sorts of urls, like icons, ajax url an so on, which means that whatever action I am going to take to customize item, will be called multiple times.

A cache does not work because item customization depened on the url of the page. So what ends up happening is that the item is incorrectly customized for the last most url which is affected by the load method.

So I get the following calls in this sequence:


1) /correct-slug (here add the information to item)
2) /some-images.png (here the information for item is incorrectly customized)
3) Render Widget


As you can see I want to avoid step (2).


I am sure I am missing something somewhere in the documentation.

Can you please help?

Best,
Fotis

Tom Boutell

unread,
Mar 10, 2015, 10:35:15 AM3/10/15
to apostr...@googlegroups.com
Sorry Fotis, I'm having a little trouble understanding the use case.
Maybe you should show some code?

apos.itemTypes[widgetType].load is called for each instance of the
widget after any document containing the widget is fetched.

This would appear to be what you want, but I may be failing to
understand what you're trying to do.
> --
> You received this message because you are subscribed to the Google Groups
> "apostrophenow" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to apostropheno...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--


THOMAS BOUTELL, DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

Fotis Paraskevopoulos

unread,
Mar 10, 2015, 10:49:37 AM3/10/15
to apostr...@googlegroups.com
Hello Tom,

Thank you for getting back to me

My problem is that myWidgetType.load is being called numerous times for a single page even though it a singleton, and there is only one instance of it.

Through debugging,  I did notice the value of req.url changed each time the "load" method was being called.

The first time the "load" method is called the req.url is the slug of the page as expected. The times after that the req.url includes file urls like image files.

This is log output when a single page is access with that widgetType and nothing else. The calls are sequential in order.


==== Start Log =====
Initializing multi with host fotis:3000 portal= portal
mongodb://localhost:27017/toyota-development
I see no data/address file, defaulting to address 0.0.0.0
I see no data/port file, defaulting to port 3000
Listening on 0.0.0.0:3000
Mongoose open
Widget =>  self.load called
Widget ==>  req.url= /page/one
Widget =>  self.load called
Widget ==>  req.url= /page/one
Widget =>  self.load called
Widget ==>  req.url= /subpage/page/one
Widget =>  self.load called
Widget ==>  req.url= /apos-editor-2/content-menu
Widget =>  self.load called
Widget ==>  req.url= /apos-editor-2/content-menu
Widget =>  self.load called
Widget ==>  req.url= /apos-editor-2/content-menu
Widget =>  self.load called
Widget ==>  req.url= /subpage/favicon.png
==== End Log =====


FP






Tom Boutell

unread,
Mar 10, 2015, 11:01:18 AM3/10/15
to apostr...@googlegroups.com
I was unable to reproduce this in the sandbox with a console.log in
the slideshow loader.

Keep in mind you could see something like this if you had slideshows
in your "global" page and some of your assets are actually 404's.

You could also see it if you're loading areas for your ancestor pages.

On Tue, Mar 10, 2015 at 10:49 AM, Fotis Paraskevopoulos

Tom Boutell

unread,
Mar 10, 2015, 11:01:56 AM3/10/15
to apostr...@googlegroups.com
You will certainly see it getting loaded when it is edited etc.

Your loader shouldn't assume it is a singleton. It should consider
caching if appropriate.

Fotis Paraskevopoulos

unread,
Mar 10, 2015, 3:22:39 PM3/10/15
to apostr...@googlegroups.com
Hello Tom,

This is a fork of the sandobx with my custom "SuperDuper" widget where you can see the issue I am having. 


If you see, because the load is called multiple times (check the output) then the initial setting of the Super Dupers :) are not persisted on the request.

Please ignore the ineficiency of loading the page on each async call, assume I am calling a different asynchronous method depedning on the "Duper" selected by the user.

Hope you can shed some light on this.

Best,
Fotis


Tom Boutell

unread,
Mar 10, 2015, 3:25:53 PM3/10/15
to apostr...@googlegroups.com
Oh I see.

You made a singleton that cares what page it's on. I totally see how
you got there. But it's actually an antipattern in Apostrophe.

Singletons are still widgets and widgets are not designed to care what
page they are on.

If you really want something that is (1) always there and (2) cares
what kind of page it's on, you should be using a page loader function,
and you should be outputting the content in the page template. You'll
find this is easy to do because you're not swimming upstream anymore.

On Tue, Mar 10, 2015 at 3:22 PM, Fotis Paraskevopoulos

Fotis Paraskevopoulos

unread,
Mar 10, 2015, 3:33:10 PM3/10/15
to apostr...@googlegroups.com
Hi Tom,

Is it possible to use a page loader function in a custom Widget? As far as I know (and I don't know that much), custom widgets do not have a page loader function. 

Also I need that UI where the user selects, or rather, configures the content, in that manner. 

Furthermore, I understand where you say output the content in the page template, but I want this sort of self contained.

Sorry to keep asking, but can you reply in A2 terms :) how you would implement this, which parts of A2 would you extend/use.


FP



Tom Boutell

unread,
Mar 10, 2015, 3:36:04 PM3/10/15
to apostr...@googlegroups.com
I would not use a widget.

I'd use apostrophe-fancy-page, and put the settings in the page
settings. Then I'd get to use schemas and I'd be done really, really
quickly.

Or if I absolutely had to, I'd just make a form in the page template,
and have it submit to a custom route that just updates properties of
the page so it amounts to the same thing.


On Tue, Mar 10, 2015 at 3:33 PM, Fotis Paraskevopoulos

Fotis Paraskevopoulos

unread,
Mar 10, 2015, 3:41:33 PM3/10/15
to apostr...@googlegroups.com
I get what you are saying now.

It seems this is going to be a huge headache here for me. Because I need to do this regardless of the page type.

The first option is a no-no because I can't migrate existing content to fancy pages without breaking a ton of functionality, the testing is inmense.

I am going to go with the second option here.

Thanks,
FP



Tom Boutell

unread,
Mar 10, 2015, 3:43:43 PM3/10/15
to apostr...@googlegroups.com
I don't think it'll be so bad Fotis, since you pretty much have to
write a custom form for a custom widget right now.

You could, actually, use apostrophe-schemas if you had a lot of data
to validate but I'm guessing you don't.


On Tue, Mar 10, 2015 at 3:41 PM, Fotis Paraskevopoulos

Tom Boutell

unread,
Mar 10, 2015, 3:44:09 PM3/10/15
to apostr...@googlegroups.com
(In 0.6, it'll be easy to extend the schema of *all* pages.)

Fotis Paraskevopoulos

unread,
Mar 10, 2015, 4:02:53 PM3/10/15
to apostr...@googlegroups.com
Lovely!

Reply all
Reply to author
Forward
0 new messages