2013-09-04 22 views
5

Posiadam poniższą klasę TypeScript.Metody wywoływania TypeScript na klasie wewnątrz zakresu funkcji 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"); 
     } 
} 

Powstały JavaScript dla metody Get jest:

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

Widziałem w dokumentacji TypeScript że mogę to zrobić:

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

co skutkuje poniżej, gdzie _this jest teraz odniesieniem do instancji BrandViewModel, ale this wewnątrz jquery .each funkcja nie jest zmieniany na _this jak mógłbym się spodziewać:

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"); 
    }; 

Zamiast zrobiłem poniżej w TypeScript:

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"); 
    } 

co daje mi wynik chciałem:

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"); 
    }; 

Czy ktoś wie bardziej odpowiedni sposób to zrobić?

+0

Czy możesz mi pomóc w sprawie mojego demo.Nie wiem, dlaczego nie może uruchomić https://stackoverflow.com/questions/46834032/uncaught-typeerror-this-delete-is-not-a-function – ziqi

Odpowiedz

11

Można to zrobić:

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

który generuje:

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

Very fajnie, dziękuję. Czy istnieje jakaś szansa na wyjaśnienie, jak to działa? tj. wie, że miejsce "_to" znajduje się poza obiema funkcjami? – Pricey

+1

To jest generowane, gdy używasz notacji strzałki tłuszczu, tj.() => {} – basarat

+0

@basarat, o co pytam, to notka z grubą strzałką jest używana dwa razy i zagnieżdżona ... jak '' ten 'zostanie umieszczony we właściwym miejscu. – Pricey

Powiązane problemy