在集合支持的原型中是否存在惰性加载getter的模式(在JavaScript/Meteor中构建反应式ORM关系)

在集合支持的原型中是否存在惰性加载getter的模式(在JavaScript/Meteor中构建反应式ORM关系),javascript,meteor,meteor-collections,Javascript,Meteor,Meteor Collections,我试图在一个收集支持的Meteor原型中模拟一个延迟加载的项目数组,但具有反应性 那么,假设我有一本书集,带有原型: Book = function(document) { this._title = document.title; this._author = document.author; // ... }; Books.prototype = { get id() { // Read-only return this._id; }, get tit

我试图在一个收集支持的Meteor原型中模拟一个延迟加载的项目数组,但具有反应性

那么,假设我有一本书集,带有原型:

Book = function(document) {
  this._title = document.title;
  this._author = document.author;
  // ...
};
Books.prototype = {
  get id() {
    // Read-only
    return this._id;
  },
  get title() {
    return this._title;
  },
  set title(value) {
    this._title = value;
  },
  // ...
};
Books = new Meteor.Collections('books', {
  transform: function(doc) {
    return new Book(doc);
  }
});
现在我想要一个书架集合,但是我想懒洋洋地装书:

Shelf = function(document) {
  this._location = document.location;
  this._floor = document.floor;
  // ...
  this._book_ids = document.book_ids;
};
Shelf.prototype = {
  get id() {
    // Read-only
    return this._id;
  },
  get location() {
    return this._location;
  },
  set location(value) {
    this._location = location;
  },
  // ...
  get book_ids() {
    // This returns an array of just the book's _ids
    return this._book_ids;
  },
  set book_ids(values) {
    this._book_ids = values;
    // Set _books to "undefined" so the next call gets lazy-loaded
    this._books = undefined;
  },
  get books() {
    if(!this._books) {
      // This is what "lazy-loads" the books
      this._books = Books.find({_id: {$in: this._book_ids}}).fetch();
    }
    return this._books;
  }
};
Shelves = new Meteor.Collections('shelves', {
  transform: function(doc) {
    return new Shelf(doc);
  }
});
所以,现在我有了一个我现在可以称之为
Shelf.books
的Self,并获得所有
书籍,但它们在我调用它之前不会加载。此外,调用设置
图书ID
将导致数据无效,因此下一次调用
图书
将生成与该
书架
关联的新的
图书集

现在,我如何使这种反应式更新
book\u id
触发回调以查找正确的
书籍,并触发任何有
书架的人。书籍现在将被触发刷新?或者,更好的是,如果一本
被更新,那么与该
相关的所有内容(书架.books
和任何调用它的人)也会得到反应性更新