Making apos.get calls available to widgets

17 views
Skip to first unread message

Matthew Crider

unread,
Aug 13, 2015, 2:17:28 PM8/13/15
to apostrophenow
I'm able to access the data for all of a set of fancy pages from any of my page templates by setting this in the pages object in app.js

    load: [
     
function(req, callback) {
       
// Loads all speakers and assigns them to the `allSpeakers` nunjuck variable
       
return site.apos.get(req, { type: 'speaker'}, {  }, function (err, results) {
         
if(err) {
            callback
(err);
         
}
         
if(results.total > 0) {
            req
.extras.allSpeakers = results.pages;
            callback
(null);
         
}
       
})
     
}
   
]

And it works great for page templates.  However, I have a custom widget (based off of the twitter widget) and I'm not able to use the allSpeakers variable inside that widget's templates (at least not in the views/*Editor.html template).  How do I make the result of a .get() call available to my widget?  Do I have to do an ajax request and load this data in asynchronously?

Thanks,
Matt

Tom Boutell

unread,
Aug 14, 2015, 9:48:12 AM8/14/15
to apostr...@googlegroups.com
You can do anything you want in the "load" method of the widget:

self.load = function(req, item, callback) { ... }

Be sure to cache the information in a property of "req" after the first time you load it if all the widgets just want to see the same data.

You can then attach the information to "item", for instance as item._allSpeakers (note the underscore which ensures it won't get stored back to mongodb with the widget somehow).

--
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

Matthew Crider

unread,
Aug 14, 2015, 11:13:27 AM8/14/15
to apostrophenow
Is there a way to get load called for the widget's editor modal?  I see load gets called when the widget is rendered to the page but I need something to hook on to for the editor so I can provide some data to the widget's settings UI.

Thanks,
Matt

Tom Boutell

unread,
Aug 17, 2015, 9:07:17 AM8/17/15
to apostr...@googlegroups.com
Oh I see.

Not out of the box, but you can make any AJAX calls you like in your override of the afterCreatingEl method (see the apostrophe-twitter module for an example of this method).

Also, depending on what you are actually trying to do...  apostrophe-schema-widgets might be totally sufficient for your needs if you include a joinByOne field that allows the user to pick a speaker.

Matthew Crider

unread,
Aug 18, 2015, 11:43:41 AM8/18/15
to apostrophenow
Hah yeah, using joins with the apostrophe-schema-widgets is exactly what I need.  I had a fairly complex use case and this handled it perfectly..  Very powerful those schema widgets!

Except I think I found a bug.  I filed it at https://github.com/punkave/apostrophe-schemas/issues/6.  Let me know if you need any additional information.

-Matt

Tom Boutell

unread,
Aug 18, 2015, 11:52:56 AM8/18/15
to apostr...@googlegroups.com
Aaaand fixed (:
Reply all
Reply to author
Forward
0 new messages