2013-09-04 4 views
5

मेरे पास नीचे टाइपस्क्रिप्ट क्लास है।jquery फ़ंक्शन स्कोप के अंदर कक्षा पर टाइपस्क्रिप्ट कॉलिंग विधियां

export class BrandViewModel { 

     private _items = ko.observableArray(); 

     public Add(id: number, name: string, active: boolean) : void { 
      this._items.push(new BrandItem(this, id, name, active)); 
     } 

     public Get() : void { 
      $.get("/api/brand", function(items) { 
       $.each(items, function (i, item) { 
         this.Add(item.Id, item.Name, item.Active); 
        }); 
      }, "json"); 
     } 
} 

Get विधि के लिए जिसके परिणामस्वरूप जावास्क्रिप्ट है:

BrandViewModel.prototype.Get = function() { 
     $.get("/api/brand", function (items) { 
      $.each(items, function (i, item) { 
       this.Add(item.Id, item.Name, item.Active); 
      }); 
     }, "json"); 
    }; 

मैं TypeScript प्रलेखन कि मैं यह कर सकता में देखा है:

public Get() : void { 
     $.get("/api/brand",() => function(items) { 
      $.each(items, function (i, item) { 
        this.Add(item.Id, item.Name, item.Active); 
       }); 
     }, "json"); 
    } 

कौन सा नीचे, जहां में जो परिणाम _this अब BrandViewModel उदाहरण का संदर्भ है लेकिन this jqueryके अंदर है TypeScript में

BrandViewModel.prototype.Get = function() { 
     var _this = this; 
     $.get("/api/brand", function() { 
      return function (items) { 
       $.each(items, function (i, item) { 
        this.Add(item.Id, item.Name, item.Active); 
       }); 
      }; 
     }, "json"); 
    }; 

इसके बजाय मैं नीचे किया है::समारोह के रूप में मैं उम्मीद कर सकते हैं _this करने के लिए बदल नहीं है

public Get(): void { 
     var _this = this; 
     $.get("/api/brand", function(items) { 
      $.each(items, function (i, item) { 
        _this.Add(item.Id, item.Name, item.Active); 
       }); 
     }, "json"); 
    } 

जो मुझे परिणाम मैं चाहता था देता है:

BrandViewModel.prototype.Get = function() { 
     var _this = this; 
     $.get("/api/brand", function (items) { 
      $.each(items, function (i, item) { 
       _this.Add(item.Id, item.Name, item.Active); 
      }); 
     }, "json"); 
    }; 

क्या कोई इसे करने के लिए एक और अधिक उचित तरीका जानता है?

+0

तुम मुझे मेरे demo.I के बारे में मदद नहीं जानता कि क्यों यह https://stackoverflow.com/questions/46834032/uncaught-typeerror-this-delete-is-not-a-function नहीं चलाए जा सकते हैं – ziqi

उत्तर

11

आप ऐसा कर सकते हैं:

public Get() : void { 
     $.get("/api/brand", (items) => { 
      $.each(items, (i, item) => { 
        this.Add(item.Id, item.Name, item.Active); 
       }); 
     }, "json"); 
    } 

कौन उत्पन्न करता है:

ECMAScript 6 arrow functions साथ
BrandViewModel.prototype.Get = function() { 
     var _this = this; 
     $.get("/api/brand", function (items) { 
      $.each(items, function (i, item) { 
       _this.Add(item.Id, item.Name, item.Active); 
      }); 
     }, "json"); 
    }; 
+0

बहुत अच्छा आपका धन्यवाद। इस तरह के काम के बारे में कुछ स्पष्टीकरण का कोई मौका क्या है? यानी दोनों कार्यों के बाहर '_this' जगह रखना जानता है? – Pricey

+1

जब आप वसा तीर नोटेशन का उपयोग करते हैं तो यह उत्पन्न होता है Ie.() => {} – basarat

+0

@ बसरत जो मैं पूछ रहा हूं वह वसा तीर नोटेशन दो बार और घोंसला का उपयोग किया जा रहा है .. सही जगह पर '_this' कैसे लगाया जाता है। – Pricey

0

अनुरूप, टाइपप्रति lexically इस बांधता है जब => का उपयोग कर।

संबंधित मुद्दे