简体   繁体   English

角流星收集onchange

[英]angular meteor collection onchange

I have 2 mongo meteor collections, named applications and sliders I have a service that subscribes to them and when they are ready, it populates an array in tree format. 我有2个mongo流星集合,命名的应用程序和滑块,我有一个订阅它们的服务,当它们准备就绪时,它将以树格式填充数组。

the problem is that when the collection changes nothing is telling the service about it. 问题在于,当集合发生更改时,什么也没有告诉服务它。 So if I print the collections and the array into html, the first time everything is synced, but after a change to the collection only the collection changes.. not the array 因此,如果我将集合和数组打印为html,则所有内容都将首次同步,但是在对集合进行更改之后,仅集合会更改..而不是数组

in short, when the collection in the db changes the meteor collection is updated, but not my "section" array 简而言之,当数据库中的集合发生变化时,流星集合就会更新,但我的“ section”数组不会更新

in my service: 为我服务:

var applications, sliders;
var sections = [{
              name: 'Getting Started',
              //state: 'home.gettingstarted',
              type: 'link'
            }];



          $meteor.subscribe('applications').then(function(subscriptionHandle) {
            applications = $meteor.collection(Applications);
          }).then(function(subscriptionHandle) {
            $meteor.subscribe('sliders').then(function(subscriptionHandle) {
              sliders = $meteor.collection(Sliders);
              createSections();
            });
          });


function createSections() {
//some login to create a tree
            for (var i = 0; i < applications.length; i++) {
              var pages = [];
              for (var j = 0; j < sliders.length; j++) {
                if (sliders[j].appId != applications[i].id())
                  continue;
                var page = {};
                page.name = sliders[j].nm;
                page.type = "link";
                page.data = sliders[j];
                pages.push(page);
              }
              sections.push({
                name: applications[i].nm,
                type: 'toggle',
                pages: pages,
                data: applications[i]
              });
            }
          }

the controller in using the service. 控制器使用服务。 the html is looping the sections. html正在循环这些部分。

You're calling createSections in the subscription callback, which will only fire one time upon subscription .ready() . 您正在订阅回调中调用createSections ,仅在订阅.ready()时触发一次。 It sounds like instead you wish to update sections each time the applications collection is modified. 听起来好像您希望每次修改应用程序集合时都更新部分。 In that case, you could use applications. 在这种情况下,您可以使用应用程序。 observeChanges which will fire each time documents are added, changed, or removed from your collection. 每次添加,更改或从您的集合中删除文档时,将触发observeChanges Something like: 就像是:

applications.observeChanges({
  added: function(id, doc) {
    // do stuff with added documents
  }
});

Don't forget that Minimongo cursors are by default reactive, so long as you call them in a reactive data context so a simple applications.find().forEach(function(doc){}) could be enough for your purposes. 不要忘了Minimongo游标默认情况下是反应性的,只要您在反应性数据上下文中调用它们,那么简单的applications.find().forEach(function(doc){})就足以满足您的目的。

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

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