可观察数组或在 Meteor 中的内存集合 [英] Observable array or in memory collection in Meteor

查看:51
本文介绍了可观察数组或在 Meteor 中的内存集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Meteor 中创建可观察数组或内存集合?

Is there a way to create an observable array or in memory collection in Meteor?

我伪造它的方法是创建一个包含数组的会话变量,Session.setDefault('people', []);然后在数组更改时更新该值,Session.set('people', modifiedArray).

The way I'm faking it is by creating a session variable containing the array, Session.setDefault('people', []); and then updating that value when the array changes, Session.set('people', modifiedArray).

推荐答案

您可以通过调用 Meteor.Collection 构造函数来创建本地集合,而无需在参数中提供集合名称,即:

You can create a local collection by calling Meteor.Collection constructor without supplying collection name in the parameter, i.e.:

LocalList = new Meteor.Collection();

请参阅Meteor 文档.

另请注意,由于 Dependencies,您可以观察任何您想要的内容.

Notice also that you can observe anything you want thanks to Dependencies.

示例:

List = function() {
    this.data = [];
    this.dep = new Deps.Dependency();
};

_.extends(List.prototype, {
    insert: function(element) {
        this.data.push(element);
        this.dep.changed();
    },
});

var list = new List();

Template.observer.helper = function() {
    list.dep.depend();
    return list.data;
};

每次调用 list.insert 函数时,

helper 都会更新,observer 模板会重新渲染.

helper will be updated and observer template will rerender each time you call list.insert function.

这篇关于可观察数组或在 Meteor 中的内存集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆