Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
5 Trả lời
5255 Lượt xem

Hi all,

Here i want return all partners in odoo to stock picking by JavaScript.

get_partners: function(){

    var self = this;

    var model = new instance.web.Model('res.partner');

    this.partners = [];

    return model.query(['id', 'name']).filter([['customer', '=', 'True']]).limit(1000).all().then(function(partners){

        _.each(partners, function(partner){

        self.partners.push(partner);

        });

        });

    },

This code perfectly fetching all partners, but doesn't return the arrays. please give me suggestions.

thanks in advance...

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

What you are returning is a promise of the deferred returned by all() that could be used when the returned promise get resolved in the same way that the function argument used on then execution. You could also create a deferred to be returned and resolve with the values of partners, that way you could return a more direct object to the results but outside of get_partners there need to be some code attached to the deferred or promise on done/then/etc to get to resolved values

===============================================================

How it will work?

get_partners: function(){
var self = this;
var model = new instance.web.Model('res.partner'); 
var result = $.Deferred();
return model.query(['id', 'name']).filter([['customer', '=', 'True']]).limit(1000).all(); 
},
use_of_get_partners: function(){
var self = this;
self.partners = [];
this.get_partners().done(function(partners){
#here you can use the partners when get_partners promise is done
_.each(partners, function(partner){
self.partners.push(partner);
});
#do what you need to do with the partners directly here
})
}

I like the same but this is the way you could get the partners outside get_partners function. Also with your code you could do the same by directly using the partners result

Ảnh đại diện
Huỷ bỏ
Tác giả

Please, give me brief explanation. thanks

all depends on how are you using your get_partners function. let me update my answer with an example of how (I think) you need to refactor your code

Tác giả

Ok, please give me some example to refactor my code

edited on the answer

Tác giả

doesn't return values

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 5 24
1044
0
thg 7 25
161
1
thg 7 25
4950
0
thg 7 25
479
0
thg 6 25
592