2013-08-16 39 views
1

有沒有一種方法來創建一個可觀察的數組或在流星的內存集合?可觀測陣列或在流星的內存集合

我僞裝它的方式是創建一個包含數組的會話變量Session.setDefault('people', []);然後在陣列更改時更新該值,Session.set('people', modifiedArray)

回答

8

你可以通過調用Meteor.Collection構造函數沒有參數提供集合名稱創建一個本地集合,即:

LocalList = new Meteor.Collection(); 

看到這個the Meteor documentation

另請注意,您可以觀察任何你想要的東西,這要感謝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; 
}; 

helper將被更新和observer模板將在每次調用list.insert功能時重新呈現。

+1

從0.9.1開始,Meteor.Collection()變成了Mongo.Collection() – user3711864