在集合支持的原型中是否存在惰性加载getter的模式(在JavaScript/Meteor中构建反应式ORM关系)
我试图在一个收集支持的Meteor原型中模拟一个延迟加载的项目数组,但具有反应性 那么,假设我有一本书集,带有原型:在集合支持的原型中是否存在惰性加载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
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
和任何调用它的人)也会得到反应性更新