dart - 为什么我的更新的可观察列表未反射(reflect)在模板中?

标签 dart polymer dart-polymer

我有:

my-app
  community-list

附加上,my-app获取用户并加载app.user。同时,附加了社区列表(甚至在加载app.user之前),因此我还无法获得该用户的已加星标社区。因此,我正在研究的解决方案如下。

在community-list.attached()中:
app.changes.listen((List<ChangeRecord> records) {
  if (app.user != null) {
    getUserStarredCommunities();
  }
});

有人说社区列表中的其他方法:
  // This is triggered by an app.changes.listen.
  void getUserStarredCommunities() {
    // Determine if this user has starred the community.
    communities.forEach((community) {
      var starredCommunityRef = new db.Firebase(firebaseLocation + '/users/' + app.user.username + '/communities/' + community['id']);
      starredCommunityRef.onValue.listen((e) {
        if (e.snapshot.val() == null) {
          community['userStarred'] = false;
        } else {
          community['userStarred'] = true;
        }
      });
    });
  }

请注意,社区是社区列表中的可观察列表:
@observable List communities = toObservable([]);

最初填充在community-list.attached()中:
getCommunities() {
    var f = new db.Firebase(firebaseLocation + '/communities');

    var communityRef = f.limit(20);
    communityRef.onChildAdded.listen((e) {
      var community = e.snapshot.val();

      // If no updated date, use the created date.
      if (community['updatedDate'] == null) {
        community['updatedDate'] = DateTime.parse(community['createdDate']);
      }

      // snapshot.name is Firebase's ID, i.e. "the name of the Firebase location"
      // So we'll add that to our local item list.
      community['id'] = e.snapshot.name();

      // Insert each new community into the list.
      communities.add(community);

      // Sort the list by the item's updatedDate, then reverse it.
      communities.sort((m1, m2) => m1["updatedDate"].compareTo(m2["updatedDate"]));
      communities = communities.reversed.toList();
    });
  }

总而言之,即使在拥有用户之前,我也要加载社区列表,但是一旦有了用户,我想使用userStarred = true / false来更新社区列表中的每个社区( map ),然后将其用于社区列表模板。
  • las,列表似乎没有更新。我该如何实现?
  • 这整个app.changes.listen业务都很昂贵。在这种情况下的正确做法是什么,即在我加载将以某种方式修改它的对象(例如app.user)之前先加载一个元素。
  • 最佳答案

    1)toList()创建列表的副本。您需要再次应用toObservable以获得可观察的列表。

    communities = toObservable(communities.reversed.toList());
    

    这还将为communities覆盖的@observable分配一个新列表。
    我认为还是应该触发

    2)您明确更新社区。不必听changes。您可以调用包含

    if (app.user != null) {
      getUserStarredCommunities();
    }
    

    每次更改列表时都会明确显示。

    communities发生更改时,您还可以为每个社区调用Firebase。我不了解Firebase,但似乎您每次都向服务器发送请求,这当然很昂贵。
    您应该记住已经进行了哪种user + community组合,并改用记住的结果。

    使用app.changes.listen,您可以侦听组件中任何@observable字段的更新。如果在communities之外还有其他可观察的字段,则此方法可能被调用得太频繁。
    如果您只对communities的更改感兴趣,则应将此代码放入类似的方法中

    communitiesChanged(oldVal, newVal) {
      if (app.user != null) {
        getUserStarredCommunities();
      }
    }
    

    但是更好的选择是不听更改和其他方法名称,并尽可能地将其显式地调用为状态。

    关于dart - 为什么我的更新的可观察列表未反射(reflect)在模板中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25633717/

    相关文章:

    dart - 使用 Dart 在核心脚手架中手动隐藏和显示抽屉面板

    flutter - 在自定义类 flutter 中使用提供程序

    firebase - 构建画廊的最佳方式 (Firestore)

    html - 是否可以在 React 中使用 Polymer?

    polymer - 如何在 Polymer 2.0 中启用 Shady DOM?

    Dartium 显示空白页

    flutter - RangeError(索引):无效值:使用异步功能,有效值范围为空

    flutter - 如何在Column中更改子窗口小部件的位置?

    javascript - 为什么我的 Polymer iron 图标出现在我的导航纸输入下方?

    dart - 最新的稳定版本(v1.1.1)-Dartium上不提供内置库 'dart:io'