Ember data: Insert new item at beginning of array instead of at the end

前端 未结 2 1189
我在风中等你
我在风中等你 2020-12-21 04:57

In my application I\'m displaying a timeline of messages. We retrieve them from the server in descending chronological order, newest to oldest.

3 - Howdy
2 -         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 05:40

    Can you use basic JavaScript to drop in the new item at a given location in the array? Not sure if this is basic ajax + js objects or full blown ember-data models but this works for the simple js array example

     var arr = [];
     arr[0] = "Jani";
     arr[1] = "Hege";
     arr[2] = "Stale";
     arr[3] = "Kai Jim";
     arr[4] = "Borge";
    
     console.log(arr.join());
     arr.splice(2, 0, "Lene");
     console.log(arr.join());
    

    The output of the code above will be:

    Jani,Hege,Stale,Kai Jim,Borge
    Jani,Hege,Lene,Stale,Kai Jim, Borge
    

提交回复
热议问题