Meteor中陣列的臨時存儲

[英]Temporary storage for array in Meteor


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上寫了一篇關於如何使用它們的文章。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/06/15/712eb72f34e9cd85d9fe1498ad39aaa5.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com