Moved paginated sub into a package

This commit is contained in:
Tom Coleman 2013-03-23 16:58:28 +11:00
parent 17304f3f1a
commit 1974ac0386
5 changed files with 12 additions and 57 deletions

View file

@ -13,3 +13,4 @@ moment
spiderable
email
database-forms
paginated-subscription

View file

@ -110,7 +110,7 @@ var sortBy = function(sortProperty){
}
var postListSubscription = function(find, options, per_page) {
var handle = paginatedSubscription(per_page, 'paginatedPosts', find, options);
var handle = Meteor.subscribeWithPagination('paginatedPosts', find, options, per_page);
handle.fetch = function() {
return limitDocuments(Posts.find(find, options), handle.loaded());
}

View file

@ -1,53 +0,0 @@
PaginatedSubscriptionHandle = function(perPage) {
this.perPage = perPage;
this._limit = perPage;
this._limitListeners = new Deps.Dependency();
this._loaded = 0;
this._loadedListeners = new Deps.Dependency();
}
PaginatedSubscriptionHandle.prototype.loaded = function() {
Deps.depend(this._loadedListeners);
return this._loaded;
}
PaginatedSubscriptionHandle.prototype.limit = function() {
Deps.depend(this._limitListeners);
return this._limit;
}
PaginatedSubscriptionHandle.prototype.loading = function() {
return this.loaded() < this.limit();
}
PaginatedSubscriptionHandle.prototype.loadNextPage = function() {
this._limit += this.perPage;
this._limitListeners.changed();
}
PaginatedSubscriptionHandle.prototype.done = function() {
// XXX: check if subs that are canceled before they are ready ever fire ready?
// if they do we need to increase loaded by perPage, not set it to limit
this._loaded = this._limit;
this._loadedListeners.changed();
}
// XXX: somehow? support the last argument being a callback for ready?
paginatedSubscription = function (perPage/*, name, arguments */) {
var handle = new PaginatedSubscriptionHandle(perPage);
var args = Array.prototype.slice.call(arguments, 1);
Meteor.autorun(function() {
var ourArgs = _.map(args, function(arg) {
return _.isFunction(arg) ? arg() : arg;
});
var subHandle = Meteor.subscribe.apply(this, ourArgs.concat([
handle.limit(), function() { handle.done(); }
]));
handle.stop = subHandle.stop;
});
return handle;
}

View file

@ -6,6 +6,7 @@
"branch": "beforeRouting"
},
"moment": {},
"database-forms": {}
"database-forms": {},
"paginated-subscription": {}
}
}

View file

@ -11,7 +11,8 @@
"branch": "beforeRouting"
},
"moment": {},
"database-forms": {}
"database-forms": {},
"paginated-subscription": {}
},
"packages": {
"router": {
@ -29,6 +30,11 @@
"tag": "v0.0.2",
"commit": "9e21c15aca103a18013b6d518fe5b116f16368f0"
},
"paginated-subscription": {
"git": "https://github.com/tmeasday/meteor-paginated-subscription.git",
"tag": "v0.1.0",
"commit": "623fe77698dbfdfdfdb52855c1ba19f29aca8047"
},
"page-js-ie-support": {
"git": "https://github.com/tmeasday/meteor-page-js-ie-support.git",
"tag": "v1.3.5",