An effective method for binding items permanently while still being able to update the entire selection

Imagine a scenario where a list of 1000 items is displayed using infinite scrolling.

Each item on the list includes a person's firstName, lastName, and mood for simplicity.

Initially, I didn't want to constantly listen for updates.
Fortunately, the amazing angular-bindonce directive or the even better angular 1.3 one-binding feature came to the rescue.

Now, I have implemented a pull-to-refresh component that allows refreshing the entire list of items.
However, with binding once and not reloading the page, my list does not take the updates into account.

Here is the current setup using angular-bindonce:

<div bindonce ng-repeat="person in persons track by person.id">
  <span bo-text="person.firstName"></span>
  <span bo-text="person.lastName"></span>
  <span bo-text="person.currentMood"></span>
</div>

The pull-to-refresh triggers this function:

$scope.refresh() {
    Persons.getList(function(response)) {
      $scope.persons = response.data;  //data being an array
    }
}

Now, the question arises:

Is there a way to refresh all the data ONLY when the pull-to-refresh is triggered?
This would enable me to maintain the one-binding approach and significantly enhance performance while dealing with large lists of persons.

Currently, I am compelled to resort to two-way binding, which is the default approach in Angular.

On a broader note, how should one handle massive lists with infinite scrolling that require updating only upon specific events?

Answer №1

Download angular-bind-notifier for your project.

Implement native bindings with a slightly tweaked syntax and structure your HTML as shown below:

<div ng-repeat="person in persons track by person.id" bind-notifier="{ eventKey:watchedExpression }">
  <span>{{:eventKey:person.firstName}}</span>
  <span>{{:eventKey:person.lastName}}</span>
  <!-- or use ng-bind if preferred -->
  <span ng-bind=":eventKey:person.currentMood"></span>
</div>

As soon as the value of watchedExpression changes, a $broadcast will be triggered within the childscope generated by bind-notifier, prompting all bindings using the :key:expr syntax to re-evaluate.


If necessary, you can manually send the $broadcast in this format:

$scope.$broadcast('$$rebind::' + key) // where 'key' corresponds to 'eventKey' as shown in the example above.

Answer №2

Update-on directive may be the solution you are looking for, I came across a helpful thread HERE:

<div bindonce="items" update-on="'update'" ng-repeat="item in items track by item.id">
  <span bo-text="item.name"></span>
  <span bo-text="item.description"></span>
  <span bo-text="item.price"></span>
</div>

Answer №3

Instead of attempting to find a workaround for not utilizing two-way binding while still enjoying its advantages, there is a more feasible and straightforward solution available. If you mention that there are 1,000 rows, is the user able to see all 1,000 rows at once within the viewport?

I would assume not, so my recommendation would be to implement a buffered view for the list of items. By buffering the rows, the ones that are not visible do not have bindings but still occupy space in the DOM, ensuring an accurate scroll bar position.

The primary drawback of buffering is that all rows must have the same height, without any variability in row heights.

For reference, here are some virtual scrolling/buffering directives worth exploring:

https://github.com/EnzeyNet/VirtualScroll

https://github.com/stackfull/angular-virtual-scroll

https://github.com/kamilkp/angular-vs-repeat

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

How jQuery manipulates the DOM during a drag-and-drop operation

My current challenge involves implementing jquery-ui sortable on items that appear while scrolling. Below is the code snippet I am using: var gridTop = 0, gridBottom = container.outerHeight(); $('#play-list').on('scroll', ...

The error message "highcharts-ng: Unable to access property 'setExtremes' of an undefined object" was encountered

Out of nowhere, the highcharts-ng module began throwing a strange error. https://i.sstatic.net/TxoVd.png I haven't made any changes to this part of the code and I don't even use the setExtremes property. Error Line 92: https://i.sstatic.net/O ...

Encountered an error while attempting to load resource: the server returned a 404 (Not Found) status code when trying to load an image in an

I am looking to dynamically load an image when it is selected from a file picker dialog. The code provided below attempts to achieve this, however, the image does not load into the img tag. <script src="https://cdnjs.cloudflare.com/ajax/libs/jq ...

Restangular is throwing an error because it cannot find the reference for _

I encountered an issue with Restangular where I'm getting the error message: Uncaught ReferenceError: _ is not defined from restangular when trying to use it. Code Snippet: <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angula ...

Whenever a menu option is clicked in Umbraco, the page always redirects to the route #/content

After updating my site to the latest version available, I encountered a problem: Whenever I click on any link in the menu on the admin page (/umbraco) The temporary solution I discovered is Click on the link Go to profile Edit Confirm that you want to ...

Having trouble changing the state within React's useEffect() when using an empty dependencies array? Socket.io is the cause

I have a question regarding the access of allUserMessages from my state within useEffect without anything in its dependency array. Let me provide more details below. Thank you. My goal is to append data to an array in my state using useEffect similar to c ...

Problem with organizing data by dates

My timers list looks like this: timer 1 => { startDate = 17/01/2019 11PM, endDate = 18/01/2019 9AM } timer 2 => { startDate = 18/01/2019 7AM, endDate = 18/01/2019 1PM } timer 3 => { startDate = 18/01/2019 12PM, endDate = 18/01/2019 10PM } time ...

unable to locate express router

Recently, I set up an express project using the express generator with the following commands: express project_name and npm install Here is a snippet from my app.js file: var express = require('express'); var path = require('path') ...

Interested in creating a Twitter bot that can automatically share images from a designated folder in sequential order. Leveraging the power of node.js

Recently, I've been following an online guide to create a Twitter bot that tweets images from a folder on my computer. While I have a vast collection of photos I'd like to share, the guide only demonstrates how to post them in a random sequence. ...

Is there a way for my component to function seamlessly within another component without the need for redundant code?

Is there a way to avoid repeating code when using my component inside another component? For example, in the file NextPage.js, the <LogoutButton/> component does not perform its function. How can I ensure that the <LogoutButton/> behaves the s ...

Deleting uploaded images in Cloudinary for Next.js is not allowed

After successfully implementing image uploads in cloudinary for Next.js, I encountered an issue with image deletion. Despite creating the necessary routes and components, the deletion functionality is not working as expected. I am unsure of what could be ...

Python Selenium: Cannot Click on Element - Button Tag Not Located

TL,DR: My Selenium Python script seems to be having trouble "clicking" on the necessary buttons. Context: Hello. I am working on automating the process of logging into a website, navigating through dropdown menus, and downloading a spreadsheet. Despite ...

How can one efficiently make API requests using Vuex?

Being new to both Vue Webpack and Vuex after transitioning from the Ember world, I have set up my Vue Webpack app with Vuex using vue-resource in two different files: /src/store/api.js import Vue from 'vue'; import { store } from './store& ...

Discover the proper technique to display an error message in cases where no data is detected during the filtering process

I'm currently working on a component that involves search filtering and dropdown filtering. The filtering functionality is working fine, but I'm struggling to determine where to display an error message if no data is found during the filtering pr ...

transferring data using react-router

I'm working on a code that displays four images, and when one of them is clicked, it should take up the entire screen. I am using React Router to implement this functionality. Here's the current code setup: App.js import React from 'react&a ...

Utilize the three.js library within a Vue component by importing it and incorporating its

Can someone please help me understand the proper way to import and utilize the three.js library in a vue component? Despite my extensive research, it seems that most individuals use the following code line to import three.js into a vue component. However, ...

The React material-table only updates and rerenders the table when the data is updated twice

Currently, I am utilizing a tool called material-table (check it out here: https://material-table.com/#/) which has been developed using React. The issue I am facing is that the data passed as a prop to material-table doesn't seem to update correctly ...

React Native ScrollView with a custom footer that adjusts its size based on the content inside (

I've implemented the code below to ensure that the blue area in image 1 remains non-scrollable when there is sufficient space, but becomes scrollable when space is limited. However, I'm facing an issue where the ScrollView component is adding ext ...

JavaScript code does not run when a page is being included

On my AngularJS-based page, I have included some additional HTML pages like this: <div data-ng-include src="includehtml"></div> Here is the JavaScript code: $scope.selectImage = function(id) {$scope.includehtml = id;} (I pass the HTML file ...

Is the object returned by the useParams hook maintained across renders?

The book that is displayed is based on the URL parameter obtained from the useParams hook. The selected book remains constant across renders unless there is a change in the value returned by the useParams hook. I am curious to find out if the object retur ...