2013-03-14 26 views
36

Przechodzę z polegania na jQuery na budowaniu aplikacji w AngularJS. Jest zalecany w kodzie jQuery i Angular z number of places do nie.

Jedną z rzeczy, za którą tęsknię, jest funkcja jQuery $ .map dla tablic. Wiem, że można to ponownie napisać przy użyciu natywnej wersji Javascript map function, ale nie jest to zaimplementowane we wszystkich przeglądarkach (zwłaszcza IE < v9).

Czy istnieje odpowiednik Angular, czy powinienem wrócić do pisania for (var x = 0; x < foo; x += 1) {...}, aby móc przerwać dodawanie jQuery?

AKTUALIZACJA Czasami wiadomo, czego szukać, to wszystko, czego potrzebujesz. Bergie mówi: "szukaj polyfills". Oto instrukcja obsługi (od załogi modernizr) za pęczek zasobów do produkcji nowoczesnych pracę kodu na starszych przeglądarkach: HTML5 Cross Browser Polyfills

+1

Można po prostu dołączyć polyfill ES5, są one proste i dokładne dla tych funkcji iteracji macierzy. – Bergi

+5

Angular nie jest biblioteką JS. Jest to framework do tworzenia aplikacji na jedną stronę. Chociaż ma on garść funkcji użytkowych (zobacz [angular.forEach] (http://docs.angularjs.org/api/angular.forEach)) nie próbuje powiedzieć, jak obsługiwać operacje JS niskiego poziomu. Do tego właśnie służy Lo-Dash/Underscore i przyjaciele. – Stewie

+0

Oprócz tego, o czym wspomniał @Stewie, po pewnym czasie Angular faktycznie poszedł i wrzucił jquery i nazwał go JQLite - więc dostajesz dużo jquery, kiedy używasz Angular, czy ci się to podoba, czy nie. Może również z niego korzystać. Mimo, że nadal istnieje wiele marnotrawstwa w takich elementach jak Forace w Angular, lepiej byłoby użyć jquery, jeśli jest obecny. http://docs.angularjs.org/api/angular.element –

Odpowiedz

26

Sprawdź tutaj: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map

Mozilla dostarczyła PolyFill Array.map dla nieobsługiwanych przeglądarek

if (!Array.prototype.map) { 
    Array.prototype.map = function(callback, thisArg) { 

    var T, A, k; 

    if (this == null) { 
     throw new TypeError(" this is null or not defined"); 
    } 

    // 1. Let O be the result of calling ToObject passing the |this| value as the argument. 
    var O = Object(this); 

    // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length". 
    // 3. Let len be ToUint32(lenValue). 
    var len = O.length >>> 0; 

    // 4. If IsCallable(callback) is false, throw a TypeError exception. 
    // See: http://es5.github.com/#x9.11 
    if (typeof callback !== "function") { 
     throw new TypeError(callback + " is not a function"); 
    } 

    // 5. If thisArg was supplied, let T be thisArg; else let T be undefined. 
    if (thisArg) { 
     T = thisArg; 
    } 

    // 6. Let A be a new array created as if by the expression new Array(len) where Array is 
    // the standard built-in constructor with that name and len is the value of len. 
    A = new Array(len); 

    // 7. Let k be 0 
    k = 0; 

    // 8. Repeat, while k < len 
    while(k < len) { 

     var kValue, mappedValue; 

     // a. Let Pk be ToString(k). 
     // This is implicit for LHS operands of the in operator 
     // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk. 
     // This step can be combined with c 
     // c. If kPresent is true, then 
     if (k in O) { 

     // i. Let kValue be the result of calling the Get internal method of O with argument Pk. 
     kValue = O[ k ]; 

     // ii. Let mappedValue be the result of calling the Call internal method of callback 
     // with T as the this value and argument list containing kValue, k, and O. 
     mappedValue = callback.call(T, kValue, k, O); 

     // iii. Call the DefineOwnProperty internal method of A with arguments 
     // Pk, Property Descriptor {Value: mappedValue, : true, Enumerable: true, Configurable: true}, 
     // and false. 

     // In browsers that support Object.defineProperty, use the following: 
     // Object.defineProperty(A, Pk, { value: mappedValue, writable: true, enumerable: true, configurable: true }); 

     // For best browser support, use the following: 
     A[ k ] = mappedValue; 
     } 
     // d. Increase k by 1. 
     k++; 
    } 

    // 9. return A 
    return A; 
    };  
} 
+0

Świetnie! bezpośredni i na miejscu. – Benmj

+1

OP ma link do tej samej strony w pytaniu ... ale może nie widział łatki? –

+0

@thesystem: Przyznaję, że przeszmuglowałem go pośpiesznie. – Benmj

13

Nie, w Angular nie ma odpowiednika. A jak już odkryłeś, nie musisz cofać się do pisania imperatywnego kodu.

Zamiast tego po prostu użyj funkcji map wbudowanej bezpośrednio w JavaScript. Jeśli potrzebujesz obsługi IE8, wstaw polyfill na początku swoich skryptów.