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

Setting up React Native on a Mac M1 device

I am currently setting up React Native on my MacBook M1 Despite having installed npm, JDK, Node, Rosetta, CocoaPod, VSCode, Android Studio, Xcode and more, when I try to run the command (npm start), the directories for iOS and Android are not present. The ...

How to create a fresh factory instance in Angular Js

I have implemented a factory in my application to retrieve a list of folders and display it on the front end. Additionally, I have a form on the front end where users can add new folders to the existing list. After adding a folder, I need to refresh my fac ...

Mongoose discovers an unexpected empty array

My task is to locate a SoldProduct with an intimeOrderNumber value of '3'. var query = { intimeOrderNumber: '3' }; However, my search result returns an empty array. SoldProduct.find(query, function(err, soldProducts) { if (err) { ...

Navigating Angular's Credit Card Input Functionality

I am looking to limit the input capacity to 16 numbers and add a space between each set of 4 numbers. After conducting an extensive search for a credit card input that allows users to enter 16 digits with a " - " or space in between, all results were for ...

Is it possible to modify the inline onkeyup function?

Looking for a solution to change the onkeyup function in a textarea element from 255 characters to 150 characters without directly editing the code? <textarea cols="50" rows="4" id="textbox" onkeyup="limitArea(this, 255, '')"></textarea ...

Issues arise when using res.send() with ExpressJS and Mongoose

Initially, I have multiple callbacks that need to be executed before sending a res.send() to construct a JSON API: app.get('/api', function (req, res){ ... function logPagesId() { console.log("load: " +pagesId); c ...

Customize the text that appears when there are no options available in an Autocomplete component using React

Recently, I came across this Autocomplete example from MaterialUI that caught my attention. https://codesandbox.io/s/81qc1 One thing that got me thinking was how to show a "No options found" message when there are no search results. ...

How can we prevent Material-UI and Redux-form from re-rendering when an option is clicked in the select input?

I am facing an issue with rendering options dynamically. Whenever I click on the select or the options, the component re-renders and changes the options. As a result, I need to click twice to select an option in the dropdown. This re-rendering is happening ...

retrieve data from JSON file

function retrieveDataFromProfiles(){ const fs = require('fs') fs.readFile('src/data/profileInfo.json', function(error, data){ if(error){ alert(error); } var profileData = JSON.parse(data); //retrieves the JSON data of ...

angular.copy reflects modifications in assigned variables

let originalCart = angular.copy($rootScope.cart); let cartCopy = angular.copy(originalCart); At some point in my code, I am making changes to the $rootScope.cart variable. However, these changes are also affecting the cartCopy variable, which I would li ...

Detecting when a directive element has not been clicked in Angular may involve checking for certain event behaviors

I am looking to enhance the functionality of the "deselect" feature in multiple directives like buttons and popovers. Specifically, I want my functions to activate when the user clicks on an element that is not part of the directive template. Currently, I ...

The table disappears when there is no data available in the database

One of my challenges involves a table that displays data retrieved from a database. The code for this is as follows: <table class="table table-hover table-bordered" style="width:300px" id="contact"> <tbody data-bind="foreach:items"> ...

Extract information from a JSON string and present it on the screen

I am a complete novice when it comes to D3 (with very little experience in JS). Here are the lines of code I am working with: <script type='text/javascript'> data ='{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.62,"qsec":16. ...

Enhancing class names in production mode with Material UI, Webpack, and React to optimize and minimize code size

webpack - v4.5+ material ui - v4.9.7 react - v16.12.1 Ordinarily, all classes should follow the pattern of the last one in the first example. However, for some unknown reason, many classes remain unchanged in production mode. Any thoughts on this issue? ...

Executing JavaScript following an Ajax request

I am faced with a situation where my HTML file utilizes a function for loading another PHP file using Ajax: function LoadContent(n,func) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xm ...

Retrieve data from a single PHP page and display it on another page

In my project, I am working with three PHP pages: index.php, fetch_data.php, and product_detail.php. The layout of my index.php consists of three columns: filter options, products panel, and detailed description. Whenever a user clicks on a product in th ...

To avoid the browser context menu from appearing when interacting with a d3.js element, simply employ a different way of clicking

Currently, I have a d3.js element plotted in the form of a rectangle: // draw rectangle svg.selectAll(".rect").append("rect") .attr("y", 10) .attr("x", 10) .attr("height", 5) .attr("width", 5) .on("contextmenu", function (d, i) { // ...

Difficulty encountered when trying to map an array to JSX - Error: Unexpected token

I am struggling to properly map an employees array to a new array using ReactJS and JSX. It seems like there is an issue with my return method. Please keep in mind that I am a complete beginner when it comes to React and ES6. Can someone guide me on how t ...

Creating custom validation in Vuetify for password confirmation is essential in ensuring that

While developing a Vue.js template, I encountered a scenario on the sign-up page where I needed to compare passwords during user registration. To achieve this, I implemented a custom validation rule in the code snippet below: <v-text-field label=" ...

The AngularJS model fails to refresh after using $state.go function

My login system is almost complete, but I am facing an issue where the model does not update automatically after a successful login. The user should be redirected to the dashboard page once they log in. Currently, everything works fine except that the mode ...