[Eg-newdevs] get_tcn function scope

Dan Briem dbriem at wlsmail.org
Thu Jun 17 14:33:33 EDT 2021


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>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://list.evergreen-ils.org/pipermail/eg-newdevs/attachments/20210617/c8d3be81/attachment-0001.html>


More information about the Eg-newdevs mailing list