[Eg-newdevs] get_tcn function scope

Dan Briem dbriem at wlsmail.org
Sat Jun 19 12:42:58 EDT 2021


get_tcn() performs a network request in the background and the function you
pass to .then() doesn't run right away - it's just set up to run when the
response comes back. The program keeps going in the meantime, so the dialog
will likely run before you get a response and assign the tcn value. To make
the dialog not happen until you get the tcn_value, you need to wrap it in
the .then() function as well.

Can I suggest an alternative approach? You can store the TCN in local
storage earlier, just like the bib ID, and grab it the same way in the
$scope.overlay_target function. This would also eliminate a need for the
first get_tcn call above the $scope.mark_as_overlay_target function.

For example:

$scope.local_overlay_target =
egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
*$scope.local_overlay_target_tcn =
egCore.hatch.getLocalItem('eg.cat.marked_overlay_tcn') || 0;*
$scope.mark_as_overlay_target = function() {

Then at the end of the function:

egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.local_overlay_target);
*egCore.hatch.setLocalItem('eg.cat.marked_overlay_tcn',$scope.local_overlay_target_tcn);*

Then in the $scope.overlay_record function you can simply grab it instead
of making a network call:

var overlay_target = $scope.local_overlay_target;
*var overlay_target_tcn = $scope.local_overlay_target_tcn;*
var live_overlay_target =
egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
*var live_overlay_target_tcn =
egCore.hatch.getLocalItem('eg.cat.marked_overlay_tcn') || 0;*

and pass it to the dialog (assuming you're adding tcn and live_tcn in
z3950_strings.tt2):

egConfirmDialog.open(
  confirm_title,
  confirm_msg,
  { id : overlay_target, live_id : live_overlay_target*,*
    *tcn : overlay_target_tcn, live_tcn: live_overlay_target_tcn* }

Finally, you can clear them at the end:

$scope.local_overlay_target = 0;
*$scope.local_overlay_target_tcn = 0;*
egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
*egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record_tcn');*

I also noticed, in the $scope.mark_as_overlay_target function, we already
have the selected item as a fleshed bre object, which includes the MARC, in
items[0]. So, if the TCN value always comes from 901 a (does it?), you
might not need the get_tcn function at all and could just get the 901 a
from items[0]['marcxml'] in the $scope.mark_as_overlay_target function:

$scope.local_overlay_target = items[0]['bibid'];





*var tcnSubfield = new MARC21.Record({  marcxml:
items[0]['marcxml']}).subfield('901', 'a');$scope.local_overlay_target_tcn
= tcnSubfield === null  ? $scope.local_overlay_target  : tcnSubfield[1];*

On Thu, Jun 17, 2021 at 5:44 PM Terran McCanna <
tmccanna at georgialibraries.org> wrote:

> Ack, hit send before I was done. I am getting the correct value in the
> console with this part, but it's not letting me grab that value from
>
>     $scope.overlay_record = function() {
>         var items = $scope.gridControls.selectedItems();
>         var overlay_target = $scope.local_overlay_target;
>         *var overlay_target_tcn = $scope.local_overlay_target_tcn;
> <--this part works and I can display it in the popup*
>         var live_overlay_target =
> egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
>
>
>
> *get_tcn(live_overlay_target).then(function(rec) {
> $scope.live_overlay_target_tcn = rec.tcn_value();
> console.log('LIVE TCN: ' + $scope.live_overlay_target_tcn); <--this grabs
> correct tcn            });*
>
> But if I try to grab $scope.live_overlay_target_tcn for the popup then it
> says it's undefined. :(
>
>
>
>
> Terran McCanna, PINES Program Manager
> ------------------------------
>
> Georgia Public Library Service | University System of Georgia
>
> 2872 Woodcock Blvd, Suite 250 l Atlanta, GA 30341
>
> (404) 235-7138 | tmccanna at georgialibraries.org
>
> http://help.georgialibraries.org | help at georgialibraries.org
>
> <https://www.facebook.com/georgialibraries>
> <https://www.twitter.com/georgialibs>
> <https://www.instagram.com/georgialibraries/>
> <https://www.twitter.com/georgialibs>
>
> Join our email list <http://georgialibraries.org> for stories of Georgia
> libraries making an impact in our communities.
>
>
>
> On Thu, Jun 17, 2021 at 5:39 PM Terran McCanna <
> tmccanna at georgialibraries.org> wrote:
>
>> I followed Dan's advice and the original bug is now working properly, and
>> I realized that since we have already set $scope.local_overlay_target_tcn
>> that we can access that in the message, but I'm still struggling to
>> understand how to get the other "live" value to appear in the pop-up
>> message.
>>
>>     $scope.overlay_record = function() {
>>         var items = $scope.gridControls.selectedItems();
>>         var overlay_target = $scope.local_overlay_target;
>>         *var overlay_target_tcn = $scope.local_overlay_target_tcn;
>> <--this part works*
>>         var live_overlay_target =
>> egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
>>
>>
>>
>> *get_tcn(live_overlay_target).then(function(rec) {
>> $scope.live_overlay_target_tcn = rec.tcn_value(); console.log('LIVE TCN: '
>> + $scope.live_overlay_target_tcn);            });*
>>
>> Terran McCanna, PINES Program Manager
>> ------------------------------
>>
>> Georgia Public Library Service | University System of Georgia
>>
>> 2872 Woodcock Blvd, Suite 250 l Atlanta, GA 30341
>>
>> (404) 235-7138 | tmccanna at georgialibraries.org
>>
>> http://help.georgialibraries.org | help at georgialibraries.org
>>
>> <https://www.facebook.com/georgialibraries>
>> <https://www.twitter.com/georgialibs>
>> <https://www.instagram.com/georgialibraries/>
>> <https://www.twitter.com/georgialibs>
>>
>> Join our email list <http://georgialibraries.org> for stories of Georgia
>> libraries making an impact in our communities.
>>
>>
>>
>> On Thu, Jun 17, 2021 at 2:46 PM Josh Stompro <stomproj at gsuite.larl.org>
>> wrote:
>>
>>> Thank you Dan, that was very helpful to understand what wasn't working.
>>>
>>> To answer your last question, we were trying to allow the get_tcn
>>> function to be more generic, so it could be used to grab the TCNs for an
>>> error message that pops up when you choose a different overlay target in
>>> another tab, then switch back to the original tab and try to overlay.  See
>>> this ticket. https://bugs.launchpad.net/evergreen/+bug/1932199
>>>
>>> So we want to pull in the TCN for both old and new overlay targets and
>>> display them to the user.
>>>
>>> Like so
>>>                 if (overlay_target != live_overlay_target) {
>>>                     var confirm_title =
>>> egCore.strings.OVERLAY_CHANGED_TITLE;
>>>                     var confirm_msg = egCore.strings.OVERLAY_CHANGED;
>>>
>>>                     if (live_overlay_target == 0) { // someone unset the
>>> target...
>>>                         confirm_title =
>>> egCore.strings.OVERLAY_REMOVED_TITLE;
>>>                         confirm_msg = egCore.strings.OVERLAY_REMOVED;
>>>                     }
>>>
>>>                     egConfirmDialog.open(
>>>                         confirm_title,
>>>                         confirm_msg,
>>>                         { id : overlay_target, live_id :
>>> live_overlay_target, id_tcn :
>>>                        * get_tcn(overlay_target)*, live_id_tcn :
>>> *get_tcn(live_overlay_target*)}
>>>
>>>
>>>
>>> Josh
>>>
>>> On Thu, Jun 17, 2021 at 1:33 PM Dan Briem via Eg-newdevs <
>>> eg-newdevs at list.evergreen-ils.org> wrote:
>>>
>>>> egCore.pcrud.retrieve is a promise. The idea is that it needs to run a
>>>> network request and you don't want your program to stall while waiting for
>>>> a response back. So, with promises, the .then() doesn't run right away -
>>>> it's like you're just setting it up to run later when the response comes
>>>> back, and in the meantime your program will continue on to the next line
>>>> while that promise is doing its thing.
>>>>
>>>> So the order things happen:
>>>>
>>>>    1. send request
>>>>    2. return $scope.thisTCN (undefined probably?)
>>>>    3. response comes back and $scope.thisTCN is set to rec.tcn_value()
>>>>
>>>> The next time it runs:
>>>>
>>>>    1. send request
>>>>    2. return $scope.thisTCN (previous value it was set to before)
>>>>    3. response comes back and $scope.thisTCN is set to rec.tcn_value()
>>>>
>>>> If you want to wrap the egCore.pcrud call in its own function and
>>>> return the value, you could just return the whole promise and then call
>>>> get_tcn(someRecordId).then() when you want to assign
>>>> $scope.local_overlay_target_tcn. For example:
>>>>
>>>> get_tcn($scope.local_overlay_target).then(function(rec) {
>>>>   $scope.local_overlay_target_tcn = rec.tcn_value();
>>>> });
>>>>
>>>> ...........
>>>>
>>>> function get_tcn(currTarget) {
>>>>   return egCore.pcrud.retrieve('bre', currTarget, {
>>>>     select: {bre: ['tcn_value']}
>>>>   });
>>>> }
>>>>
>>>> This assumes you aren't using $scope.thisTCN anywhere else and can just
>>>> remove that assignment.
>>>>
>>>> I'm also curious, because I looked at the patch you shared before the
>>>> meeting, what wasn't working about it? (the reason I ask is because this
>>>> should be close to the same thing)
>>>>
>>>> On Thu, Jun 17, 2021 at 1:12 PM Terran McCanna via Eg-newdevs <
>>>> eg-newdevs at list.evergreen-ils.org> wrote:
>>>>
>>>>> Okay, gang. I'm missing something and it's probably obvious to someone
>>>>> more familiar with chained functions and scope, but I can't figure it out.
>>>>> After trying a bunch of different approaches, this is the closest I've been
>>>>> able to get. It's writing the correct value to the console log, but it is
>>>>> displaying the *previous* value in the interface. Argh. See video demo of
>>>>> the problem:
>>>>>
>>>>> https://screencast-o-matic.com/watch/cr1bDNV16uo
>>>>>
>>>>> **********
>>>>>
>>>>>     $scope.local_overlay_target =
>>>>> egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
>>>>>     if($scope.local_overlay_target) {
>>>>>         var currTarget = $scope.local_overlay_target;
>>>>>        * $scope.local_overlay_target_tcn = get_tcn(currTarget);*
>>>>>     }
>>>>>     $scope.mark_as_overlay_target = function() {
>>>>>         var items = $scope.gridControls.selectedItems();
>>>>>         if ($scope.local_overlay_target == items[0]['bibid']) {
>>>>>             $scope.local_overlay_target = 0;
>>>>>             $scope.local_overlay_target_tcn = 0;
>>>>>         } else {
>>>>>             $scope.local_overlay_target = items[0]['bibid'];
>>>>>             var currTarget = items[0]['bibid'];
>>>>>             *$scope.local_overlay_target_tcn = get_tcn(currTarget);*
>>>>>         }
>>>>>
>>>>> egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.local_overlay_target);
>>>>>     }
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> *    function get_tcn(currTarget) {
>>>>> egCore.pcrud.retrieve('bre', currTarget, {            select: {bre:
>>>>> ['tcn_value']}        }).then(function(rec) {            $scope.thisTCN =
>>>>> rec.tcn_value();            console.log("TEST: " + $scope.thisTCN);
>>>>> });        return $scope.thisTCN;    };*
>>>>>
>>>>> **********
>>>>>
>>>>> This writes the correct TCN value to the console, but it does not
>>>>> always load that value to $scope.local_overlay_target_tcn. If I have an
>>>>> item marked for overlay and I load the page, it displays the correct TCN.
>>>>> But, if I use the interface to select a different item, it keeps the
>>>>> original TCN in the display.
>>>>>
>>>>> When I tried setting a regular variable (instead of a $scope variable)
>>>>> in the "then" part of the chained function, I couldn't access it from
>>>>> outside that part of the function. Am I creating the thisTCN variable
>>>>> wrong? Or returning it wrong? Or ... ???
>>>>>
>>>>>
>>>>> Terran McCanna, PINES Program Manager
>>>>> ------------------------------
>>>>>
>>>>> Georgia Public Library Service | University System of Georgia
>>>>>
>>>>> 2872 Woodcock Blvd, Suite 250 l Atlanta, GA 30341
>>>>>
>>>>> (404) 235-7138 | tmccanna at georgialibraries.org
>>>>>
>>>>> http://help.georgialibraries.org | help at georgialibraries.org
>>>>>
>>>>> <https://www.facebook.com/georgialibraries>
>>>>> <https://www.twitter.com/georgialibs>
>>>>> <https://www.instagram.com/georgialibraries/>
>>>>> <https://www.twitter.com/georgialibs>
>>>>>
>>>>> Join our email list <http://georgialibraries.org> for stories of
>>>>> Georgia libraries making an impact in our communities.
>>>>>
>>>>>
>>>>>
>>>>> On Wed, Jun 16, 2021 at 4:36 PM Josh Stompro via Eg-newdevs <
>>>>> eg-newdevs at list.evergreen-ils.org> wrote:
>>>>>
>>>>>> Terran, I think the get_tcn function is in scope... it just doesn't
>>>>>> return a value.  I followed the code in the developer console and it does
>>>>>> grab the tcn.
>>>>>>
>>>>>> The function sets the $scope.local_overlay_target_tcn to be equal to
>>>>>> the found tcn value.  But it doesn't return it.
>>>>>>
>>>>>>     function get_tcn(currTarget) {
>>>>>>         egCore.pcrud.retrieve('bre', currTarget, {
>>>>>>             select: {bre: ['tcn_value']}
>>>>>>         }).then(function(rec) {
>>>>>>             $scope.local_overlay_target_tcn = rec.tcn_value();
>>>>>>         });
>>>>>>         return;
>>>>>>     };
>>>>>>
>>>>>> Maybe the function should just return the value... and then instead
>>>>>> of just calling it like
>>>>>>
>>>>>>   } else {
>>>>>>             $scope.local_overlay_target = items[0]['bibid'];
>>>>>>             var currTarget = items[0] ['bibid'];
>>>>>>             get_tcn(currTarget);
>>>>>>         }
>>>>>>
>>>>>> You would use
>>>>>>
>>>>>>   } else {
>>>>>>             $scope.local_overlay_target = items[0]['bibid'];
>>>>>>             var currTarget = items[0] ['bibid'];
>>>>>>             $scope.local_overlay_target_tcn = get_tcn(currTarget);
>>>>>>         }
>>>>>>
>>>>>> Josh
>>>>>> [image: Company logo]
>>>>>> *Josh Stompro*
>>>>>>
>>>>>> *IT Director stomproj at gsuite.larl.org <stomproj at gsuite.larl.org>*
>>>>>> *218-233-3757 ext. 139* | *Mobile: 218-790-2110*
>>>>>>
>>>>>> *Lake Agassiz Regional Library | **www.larl.org
>>>>>> <http://www.larl.org>*
>>>>>> 118 5th ST S
>>>>>> Moorhead MN 56560
>>>>>> *Celebrating 60 Years of Service in 2021!*
>>>>>> _______________________________________________
>>>>>> Eg-newdevs mailing list
>>>>>> Eg-newdevs at list.evergreen-ils.org
>>>>>> http://list.evergreen-ils.org/cgi-bin/mailman/listinfo/eg-newdevs
>>>>>>
>>>>> _______________________________________________
>>>>> Eg-newdevs mailing list
>>>>> Eg-newdevs at list.evergreen-ils.org
>>>>> http://list.evergreen-ils.org/cgi-bin/mailman/listinfo/eg-newdevs
>>>>>
>>>>
>>>>
>>>> --
>>>> Dan Briem
>>>> Harrison Public Library
>>>> 2 Bruce Ave. Harrison, NY 10528
>>>> (914) 835-0324
>>>> harrisonpl.org <https://www.harrisonpl.org>
>>>> _______________________________________________
>>>> Eg-newdevs mailing list
>>>> Eg-newdevs at list.evergreen-ils.org
>>>> http://list.evergreen-ils.org/cgi-bin/mailman/listinfo/eg-newdevs
>>>>
>>>
Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://list.evergreen-ils.org/pipermail/eg-newdevs/attachments/20210619/68f5d8d3/attachment-0001.html>


More information about the Eg-newdevs mailing list