Endless loading with Restangular

Currently, I am using a Restangular collection which is functioning perfectly:

$scope.threads = Restangular.all('thread').getList({ limit: 10, offset: 0 }).$object;

However, my goal is to enhance the functionality by implementing a feature where additional thread models can be loaded when the user triggers it, either through scrolling to the bottom of the list or by clicking a 'Load more' button. Whenever I call .getList() again with a new offset value, it simply replaces the entire collection:

$scope.threads.getList({ limit: 10, offset: 10 });

Is there a solution available that allows the newly returned models to be added or pushed onto the existing array instead of replacing them entirely?

Answer №1

If you want to optimize performance, consider using Promises supported by Restangular. Once the data is received, you can utilize Array.push() method to add the results to $scope.threads

Here's an example code snippet:

$scope.threads = Restangular.all('thread').getList({ limit: 10, offset: 0 }).then(function(resultThreads){
$scope.threads.push(resultThreads);
});

To ensure smooth loading of additional data, keep track of your offset for fetching the next set either upon scrolling or clicking 'Load more'. In scenarios involving a large dataset, it might be beneficial to remove items from the beginning of the array while adding new ones at the end.

Angular Promise

Restangular

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Searching for image labels and substituting the path string

Upon each page load, I am faced with the task of implementing a script that scans through the entire page to identify all image src file paths (<img src="/RayRay/thisisianimage.png">) and then add a specific string onto the file paths so they appear ...

Insert a scrollbar into the new popup window on Internet Explorer after the window has been opened

I am looking to enable scrolling in a pop-up window after it has been opened. While FireFox offers the 'window.scrollbars' property for this, Internet Explorer does not have a similar feature. Is there a way to add scrolling functionality in IE u ...

What strategies can be used to effectively manage multiple asynchronous requests?

I have two requirements: one is to load an image (which will take a long time on the backend server), and the other is to perform an AJAX request. I would like it to be structured like this: $.when( [perform image loading] [perform ajax request] ).t ...

Using Google Maps to generate a polyline from an array

I have an array filled with various coordinates and data entries. My goal is to create a polyline that connects all these coordinate points within the array. While I can successfully place markers and infowindows, I am encountering difficulty in generating ...

Tips on implementing Dropdown functionality using ng-model in Selenium WebDriver

I have been experimenting with AngularJS ng-model to create a dropdown in my Selenium WebDriver code, but I am facing issues with it not working as expected. Here is the snippet of my code: package automation.test; import java.io.File; import org.openqa.s ...

The appearance of all input forms in MaterializeCSS when used in an electron environment appears to

After following the instructions here on how to get started with Materialize CSS, I am having issues with my input form appearing strange: https://i.sstatic.net/lveS2.png The contents of my current index.html file are as follows: <!DOCTYPE html> &l ...

Using ng-disabled on input elements within an ng-repeat directive

Showing different inputs based on an array looks like this <div data-ng-repeat="n in langInput.values"> <input type="text" id="auction_name_{{n.selected}}" class="form-control" name="auction_name_{{$index}}" ...

Directive in AngularJS fails to trigger upon form reset

I encountered an issue with a directive used within a form, specifically when clicking on a button with the type reset. The problem can be replicated on this stackblitz link. The directive I'm using is quite simple - it sets the input in an error stat ...

The 'log' property cannot be found on the type '{ new (): Console; prototype: Console; }' - error code TS2339

class welcome { constructor(public msg: string){} } var greeting = new welcome('hello Vishal'); Console.log(greeting.msg); I encountered an error at the Console.log statement. The details of the error can be seen in the image attached below. ...

Unable to locate properties "offsetHeight" or "clientHeight" within a React/Next.js project developed in TypeScript

I have a unique customized collapsible FAQ feature that adjusts its height based on whether it's expanded or collapsed. import { useState, useRef, useEffect } from "react"; export default FAQItem({title, description}: FAQItemProps) { cons ...

Advancing the utilization of custom Angular input fields

I've developed a unique Angular input element that utilizes a textarea as its primary input field. Is there a way for me to pass along the enter key event to the main form? ...

The expression `Object.prototype.toString.call(currentFruit) === "[object Date]"` checks if the object referenced by `current

Hi, I'm just starting to learn JavaScript and I have a question about an if condition that I came across in my code. Can someone please explain what this specific if condition does? Object.prototype.toString.call(currentFruit) === "[object Date]& ...

Retrieving values of checked boxes in a table using JavaScript

While using the select method, I had this line of code: Person_name = $('#selectedPerson').val() When selecting 2 values, person_name.length displays as 2. Recently, I switched to checkboxes displayed in a table. However, when I use person_name ...

jQuery draggable elements can be easily dropped onto droppable areas and sorted

I need help with arranging the words in the bottom tiles by sorting them from "Most Like Me" to "Least Like Me" droppable areas. Currently, I am able to drag and drop the words into different boxes, but it ends up stacking two draggable items on top of eac ...

The Bootstrap datepicker functions properly when used in plain HTML, but encounters issues when implemented within the .html()

Using HTML: <input class="m-ctrl-medium date-picker" size="16" type="text" id='lateETD1' name="dateRecv" value=""/> Not working with script: var ETD = $("#ETD"); ETD.html("<input class='m-ctrl-medium date-picker' id='la ...

Creating diverse options using attribute-value pairs

I am in need of creating an array that contains all possible combinations of attribute values. Here is an example of my attributes/values object: let attr = { color: ['red', 'green', 'blue'], sizes: ['sm&apo ...

sending a pair of parameters from two dropdown menus to a controller method in Grails

I am currently working on a project where I have two dropdown lists - one depends on the other for its values. I also have a button where I need to pass the selected values from both dropdowns to a controller function for a query. Does anyone have any adv ...

Is there a way to modify the style within a TS-File?

I've created a service to define different colors and now I want to set separate backgrounds for my columns. However, using the <th> tag doesn't work because both columns immediately get the same color. Here's my code: color-variatio ...

Resolving the issue of "Mixed Active Content" CORS error detection

When using Firefox, if JavaScript tries to make a Cross-Origin Request (CORS) to an http server from a page hosted on https, an error is thrown: Blocked loading mixed active content I am trying to capture these errors, but I'm having trouble. For ex ...

EC2 experiencing issues with Node cron execution

My setup involves running python and node scripts as cron jobs on EC2. Interestingly, the python scripts are executing without any issues, but the node scripts seem to be failing. When I manually activate the node scripts from the command line, they work p ...