简体   繁体   English

绑定一次但允许刷新整个项目的有效方法

[英]Efficient way to bind once but allowing to refresh the whole items

Let's suppose a list of 1000 items displayed with infinite scrolling. 让我们假设一个包含无限滚动的1000个项目的列表。

Each item displays: a person's firstName, lastName, and mood. 每个项目显示:一个人的firstName,lastName和心情。 (to make it simple) (简单来说)

Initially, I didn't want to listen for updates. 最初,我不想听更新。
So the great angular-bindonce directive or even better: angular 1.3 one-binding feature made the trick. 所以伟大的angular-bindonce指令甚至更好:angular 1.3单绑定功能成为了诀窍。

Now, I created a pull-to-refresh component, allowing to refresh the whole items. 现在,我创建了一个pull-to-refresh组件,允许刷新整个项目。
However, as binding once, (and not reloading the page) my whole list didn't take the updates in account. 但是,由于绑定一次,(并没有重新加载页面)我的整个列表没有考虑更新。

Using angular-bindonce, I have this currently: 使用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: pull-to-refresh触发此功能:

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

Question is: 问题是:

Is there a way to refresh all the data ONLY when the pull-to-refresh is triggered? 有没有办法在触发刷新时刷新所有数据?
In this case, I would be able to keep this one-binding that would greatly improve performance when dealing with huge lists of persons. 在这种情况下,我将能够保持这种一键式绑定,这将大大提高处理庞大的人员列表时的性能。

Until now, I'm forced to....use two-way binding, the natural way of Angular works. 直到现在,我被迫......使用双向绑定,这是Angular工作的自然方式。

More generally, how to deal with huge lists with infinite scrolling that needs to be updated only when some events are triggered? 更一般地说,如何处理具有无限滚动的巨大列表,只有在触发某些事件时才需要更新?

Get angular-bind-notifier . 获取angular-bind-notifier

Use native bindings (with a somewhat modified syntax) and setup your markup like so: 使用本机绑定(稍微修改一下语法)并设置标记,如下所示:

<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 with ng-bind if you prefer that -->
  <span ng-bind=":eventKey:person.currentMood"></span>
</div>

Now, whenever the value of watchedExpression changes - a $broadcast will be sent down through the childscope created by bind-notifier and tell every binding with the :key:expr syntax to re-evaluate. 现在,每当watchedExpression的值发生变化时, $broadcast将通过bind-notifier创建的子视图向下发送,并使用:key:expr语法告诉每个绑定重新评估。


If you need to, you can also send the $broadcast manually in the following format: 如果需要,您还可以按以下格式手动发送$broadcast

$scope.$broadcast('$$rebind::' + key) // where 'key' === 'eventKey' in the example above.

refresh-on directive could do the trick, found a reference HERE : refresh-on指令可以做到这一点,在这里找到一个参考:

<div bindonce="persons" refresh-on="'refresh'" 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>

Instead of trying to work around not using two-way binding but still have all of its benefits there is more likely and easier solution. 不是试图不使用双向绑定而是仍然具有其所有好处,而是更容易和更容易的解决方案。 You say that there are 1,000 rows, are all 1,000 rows with the viewport / visible to the user at once? 你说有1000行,所有1000行都有视口/一次对用户可见?

I would assume not, so I would suggest using a buffered view for the list of items. 我不会假设,所以我建议使用缓冲视图作为项目列表。 Buffering the rows would mean that the rows that are not visible have no bindings but still take up space in the DOM so the scroll bar is always accurate. 缓冲行意味着不可见的行没有绑定但仍占用DOM中的空间,因此滚动条始终准确。

The one major caveat of buffering is that all rows should be the same height, no variable height rows. 缓冲的一个主要警告是所有行应该是相同的高度,没有可变高度的行。

Here are some virtual scrolling / buffering directives to take a look at: 这里有一些虚拟滚动/缓冲指令来看一看:

https://github.com/EnzeyNet/VirtualScroll https://github.com/EnzeyNet/VirtualScroll

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM