Meteor中阵列的临时存储

时间:2022-06-01 12:43:49

I am using ReactiveDict to change the state and styling of one or more templates. I would like to save the id of each selected template into an array that I can then use in a method. Before I trigger this event, user needs to be able to add and remove any id from the array until determined final.

我正在使用ReactiveDict来更改一个或多个模板的状态和样式。我想将每个选定模板的id保存到一个数组中,然后我可以在方法中使用它。在我触发此事件之前,用户需要能够添加和删除数组中的任何id,直到确定为final。

How can I create an array with all the ids whose template.instance().state is set to isSelectedId?

如何创建一个包含其template.instance()。state设置为isSelectedId的所有id的数组?

I thought I could use array.push() on each click and save to a Session variable, but the Session variable does not change. I also then don't have a way to remove the specific id if unselected. I also considered saving to a collection, but I only want temporary storage.

我想我可以在每次单击时使用array.push()并保存到Session变量,但Session变量不会更改。如果未选中,我也没有办法删除特定的id。我还考虑过保存到一个集合,但我只想要临时存储。

EDITED CODE TO INCLUDE PART OF ANSWER USING LOCAL COLLECTION

编辑代码包括使用本地收集回答的部分内容

<template name="App">
    <div id="app-inner">
        {{#each companies}}
            {{>Company}}
        {{/each}}
    </div>
</template>

<template name="Company">
    <div class="company {{isSelected}}">
        <a>Company: {{companyName}}</a>
    </div>
</template>

Template.App.helpers({
    companies: function() {
        return Companies.find({});
    }
});

Template.Company.events({
    'click .company': function(e) {
        var state = Template.instance().state.get('isSelectedId');
        var id = this._id;
        var count = localSelections.find().count();
        if(count === 0) {
            var localId = localSelections.insert({});
        }
        console.log("Local ID: ", localId);
        switch (state) {
            case null:
                Template.instance().state.set('isSelectedId', this._id);
                localSelections.update({_id:localId},{$push:{select:id}});
                break;
            case id:
                Template.instance().state.set('isSelectedId', null);
                localSelections.update({_id:localId},{$pull:{select:id}});
                break;
        }
    }
});

Template.Company.helpers({
    'isSelected': function() {
        return this._id === Template.instance().state.get('isSelectedId') ? 'is-selected' : '';
    }
});

Template.Company.onCreated(function() {
    this.state = new ReactiveDict;
    this.state.set('isSelectedId', null);
});

//client/collections
    localSelections = new Mongo.Collection(null);

Meteor.methods({
    valuationAdd: function(array) {
        check(array, Array);
        Valuations.insert({selections:array});
    }
});

1 个解决方案

#1


0  

Temporary client-side storage is easy to create in a local (unmanaged) collection

临时客户端存储很容易在本地(非托管)集合中创建

myLocalCollection = new Mongo.Collection(null);

myLocalCollection = new Mongo.Collection(null);

Such a collection has all the normal features of a collection except:

这样的集合具有集合的所有常规功能,除了:

  • It never gets synchronized to the server
  • 它永远不会同步到服务器

  • You can update or delete multiple documents at a time with {multi: true}
  • 您可以使用{multi:true}一次更新或删除多个文档

I recently wrote an article on TheMeteorChef about how to use these.

我最近在TheMeteorChef上写了一篇关于如何使用它们的文章。

#1


0  

Temporary client-side storage is easy to create in a local (unmanaged) collection

临时客户端存储很容易在本地(非托管)集合中创建

myLocalCollection = new Mongo.Collection(null);

myLocalCollection = new Mongo.Collection(null);

Such a collection has all the normal features of a collection except:

这样的集合具有集合的所有常规功能,除了:

  • It never gets synchronized to the server
  • 它永远不会同步到服务器

  • You can update or delete multiple documents at a time with {multi: true}
  • 您可以使用{multi:true}一次更新或删除多个文档

I recently wrote an article on TheMeteorChef about how to use these.

我最近在TheMeteorChef上写了一篇关于如何使用它们的文章。