Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Javascript query [ deferred values ] - how to get them?

By
Odooveloper (by Rui Franco)
on 12/23/14, 9:05 PM 1,536 views

Taking in consideration the following code, how can I populate the 'lat' and 'lng' variables? I can only get a value inside the "query". :|

 

var lat=0;

var lng=0

if (coords){
                var coords_obj = new openerp.Model('gps_base.coords');
                coords_obj.query(['latitude_aux', 'longitude_aux'])
                     .filter([['id', '=', coords]])
                     .limit(1)
                     .all().then(function (result) {
                            _.each(result, function(item) {
                                lat=item.latitude_aux;
                                lng=item.longitude_aux;
                            });
                        });
                alert(lat); //shows 'undefined'
            }

0
Shawn Varghese
On 12/24/14, 4:17 AM

From my understanding of jQuery, .then() is an asynchronous call. This means that, the .then() function will be called after the coords_obj query returns a result, but any code outside will be carried out without waiting for query result.

The following line:

alert(lat);

, is outside the .then() function. Therefore, it gets called before the query returns a result. But the wierd part is that it doesn't even show the default value 0 . I noticed that console.log(lat) works in such cases, instead of using alert(lat).

Try modifying the code like this:

 

if (coords){

                var coords_obj = new openerp.Model('gps_base.coords');
                coords_obj.query(['latitude_aux', 'longitude_aux'])
                     .filter([['id', '=', coords]])
                     .limit(1)
                     .all().then(function (result) {
                            _.each(result, function(item) {
                                lat=item.latitude_aux;
                                lng=item.longitude_aux;

self.latLngFunction(lat,lng); //This way, lat and lng are displayed correctly


                            });
                        });
            }
,

// After the above function body, create a new function similar to the one below:

latLngFunction:function(lat,lng)
        {
            console.log("Latitude",lat); //
You can view this result in your browser console
            console.log("Longitute",lng);
            //
OR
            alert(lat); //Now the alert should display a proper value.
            alert(lng);
        },

 

Please let me know if this doesn't work.

Shawn Varghese
on 12/24/14, 4:20 AM

The deferred and resolve calls that you have used seem correct. This is the cleaner way to do it. However, I have faced issues with using these calls in the past - I think I am missing something as well. Can you try using console.log(query_res.latitude_aux); instead of alert(query_res.latitude_aux), just to confirm that the issue does not reside with the alert() dialog.

Shawn Varghese
on 12/25/14, 7:05 AM
0

Many thanks Swahn but no, it didn't work. You did set me in the proper direction and, after looking for widget code in the Odoo modules, I eventually came to write this:

          function get_coordinates(coords_id){

                var result = new $.Deferred();
                new instance.web.Model('gps_base.coords')
                    .query(['latitude_aux', 'longitude_aux'])
                    .filter([["id", "=", coords_id]])
                    .first()
                    .then(function(records){
                        if(!records || records.length === 0){
                            result.reject();
                        }else{
                            result.resolve(records);
                        }
                    });
                return result;
            }

 

            if (coords){
                query_res=get_coordinates(coords);
                alert(query_res.latitude_aux);
            }

 

But, unfortunately, I never get a proper value. I'm lost. It seems no matter what example I follow, it never works. Has to be something really silly I am missing here... :(

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/23/14, 9:05 PM
Seen: 1536 times
Last updated: 3/16/15, 8:10 AM