2012-03-16 4 views

답변

-1

같은 가격을 참조에서

   var price; 
       var idProd = data.prodotto_id; 

       var cb = function (o,s,r){ 

        price = Ext.util.JSON.decode(r.responseText).price; 
        console.log(price); 
       }; 
        Ext.Ajax.request({ 
         url : "index.php", 
         params : { 
          m : "Prodotti", 
          a : 'prod-price-byquantity', 
          idProd : idProd, 
          quantity: qta 
         }, 
         callback : cb, 
         scope : this 
        }); 


       console.log(price); 

는 "가격"변수는 그래서 "CB"기능 가격 내부의 값을 할당 this.price = "bla bla"를 사용해야 만하므로 클래스/네임 스페이스 내에서 가격이 사용 가능하고 수정할 수 있습니다.

그런 다음 console.log (this.price);를 사용해야합니다. 가격을 출력합니다.

+2

전혀. 'price' 변수는'cb' 함수 밖에서 정의되고 주어진 코드 스 니펫 주위에서 완벽하게 접근 할 수 있습니다. –

+1

나를 고쳐 주셔서 감사합니다. 르네. –

+0

this.price로 drfanai 수정을 시도했지만 작동하지 않습니다 ... –

1

Ajax 요청이 비동기 적이므로 콜백 기능이 즉시 호출되지 않기 때문입니다. 여기가는 방법은 다음과 같습니다

var price; // = undefined; 
Ext.Ajax.request(); 
// The request is sent and the function immediately returns 
console.log(price); // undefined 
... 
some time passes 
... 
// Finally the request finishes and your callback function is called; 
price = Ext.util.JSON.decode(r.responseText).price; 
console.log(price); // some new value 

그래서 당신이 AJAX 호출이 완료 후 가격 변수 을 얻고있다.

+0

내 코드에서 콜백 함수 안에 변수 내용이 있습니다 ... 콜백 함수가 아약스 응답 후에 호출됩니다 ... 당신이 그것에 대해 어떻게 생각하십니까? –

1

이 작품

fetchRow:function(params){ 
    var me = this; 
    var v; 
    this.fetchRowRequest(params,function(value){ 
     v = value; 
    }); 
    return v; 
} 
fetchRowRequest: function(params,callback){ 
    var me = this; 
    var record; 
    Ext.Ajax.request({ 
     url: me.proxy.api.read, 
     params: {filters:Ext.encode(params)}, 
     async:false, 
     success: function(response){ 
      var response = Ext.decode(response.responseText); 
      var row = response.rows[0]; 
      if(row){ 
       var record = Ext.create(me.model.prototype.modelName); 
       record.set(row); 
      }else{ 
       record = false; 
      } 
      callback(record); 
     } 
    }); 
}