Skip to content
Browse files

Pagination, updated readme, bug fixes

  • Loading branch information...
1 parent 6af6407 commit 0b142d8262e601a1c217b98fd7b7f2fe08ccd81e @marccampbell committed Aug 17, 2011
Showing with 69 additions and 24 deletions.
  1. +0 −2 README.md
  2. +28 −15 lib/http/routes/index.js
  3. +11 −4 lib/http/routes/json.js
  4. +8 −0 lib/http/views/model.jade
  5. +4 −2 lib/http/views/models.jade
  6. +17 −0 lib/mongoose-admin.js
  7. +1 −1 package.json
View
2 README.md
@@ -10,10 +10,8 @@ $ npm install mongoose-admin
## Known Issues
- - date fields are not editable
- nested objects which contain nested objects cannot be created on new
documents
- - not compatible with app-cluster.js yet
- no pagination of documents when listing
View
43 lib/http/routes/index.js
@@ -45,6 +45,10 @@ exports.logout = function(req, res) {
};
exports.model = function(req, res) {
+ var query = querystring.parse(Url.parse(req.url).query);
+ var start = query.start ? parseInt(query.start) : 0;
+ var count = query.count ? parseInt(query.count) : 50;
+
var adminUser = req.session._mongooseAdminUser ? MongooseAdmin.userFromSessionStore(req.session._mongooseAdminUser) : null;
if (!adminUser) {
res.redirect('/login');
@@ -57,24 +61,33 @@ exports.model = function(req, res) {
if (err) {
res.redirect('/error');
} else {
- MongooseAdmin.singleton.listModelDocuments(req.params.modelName, 0, 50, function(err, documents) {
+ MongooseAdmin.singleton.modelCounts(req.params.modelName, function(err, totalCount) {
if (err) {
res.redirect('/');
} else {
- var config = MongooseAdmin.singleton.pushExpressConfig();
- res.render('model',
- {layout: 'adminlayout.jade',
- locals: {
- 'pageTitle': 'Admin - ' + model.modelName,
- 'models': models,
- 'modelName': req.params.modelName,
- 'model': model,
- 'listFields': options.list,
- 'documents': documents,
- 'rootPath': MongooseAdmin.singleton.root
- }
- });
- MongooseAdmin.singleton.popExpressConfig(config);
+ MongooseAdmin.singleton.listModelDocuments(req.params.modelName, start, count, function(err, documents) {
+ if (err) {
+ res.redirect('/');
+ } else {
+ var config = MongooseAdmin.singleton.pushExpressConfig();
+ res.render('model',
+ {layout: 'adminlayout.jade',
+ locals: {
+ 'pageTitle': 'Admin - ' + model.modelName,
+ 'models': models,
+ 'totalCount': totalCount,
+ 'modelName': req.params.modelName,
+ 'model': model,
+ 'start': start,
+ 'count': count,
+ 'listFields': options.list,
+ 'documents': documents,
+ 'rootPath': MongooseAdmin.singleton.root
+ }
+ });
+ MongooseAdmin.singleton.popExpressConfig(config);
+ }
+ });
}
});
}
View
15 lib/http/routes/json.js
@@ -30,14 +30,21 @@ exports.documents = function(req, res) {
return;
} else {
var query = querystring.parse(Url.parse(req.url).query);
- MongooseAdmin.singleton.listModelDocuments(query.collection, query.start, query.count, function(err, documents) {
+ MongooseAdmin.singleton.modelCounts(query.collection, function(err, totalCount) {
if (err) {
res.writeHead(500);
res.end();
} else {
- res.writeHead(200, {"Content-Type": "application/json"});
- res.write(JSON.stringify(documents));
- res.end();
+ MongooseAdmin.singleton.listModelDocuments(query.collection, query.start, query.count, function(err, documents) {
+ if (err) {
+ res.writeHead(500);
+ res.end();
+ } else {
+ res.writeHead(200, {"Content-Type": "application/json"});
+ res.write(JSON.stringify({'totalCount': totalCount, 'documents': documents}));
+ res.end();
+ }
+ });
}
});
}
View
8 lib/http/views/model.jade
@@ -22,6 +22,14 @@ div#content.container_16.clearfix
- }
- }
p
+ | Viewing #{start + 1} - #{start + documents.length} of #{totalCount}
+ |   -  Page: 
+ - for (var i = 0, page = 1; i < totalCount; i += count, page++)
+ - if (start == i)
+ #{page}
+ - else
+ a(href='#{rootPath}/model/#{modelName}?start=#{i}') #{page}
+ p
| <a href='#{rootPath}/model/#{modelName}/document/new'>Create new <em>"#{modelName}"</em> item...</a>
div.grid_5
h2 Recent Activity
View
6 lib/http/views/models.jade
@@ -7,9 +7,10 @@ script(type='text/javascript')
data: 'collection=' + encodeURIComponent($(this).attr('rel')) +
'&start=0&count=5',
success:function(results) {
+ $('#viewAll_' + self.attr('rel')).html('View All ' + results.totalCount + '...');
self.html('');
var table = $('<table />');
- $.each(results, function(k, v) {
+ $.each(results.documents, function(k, v) {
var row = $('<tr />');
var i = 0;
for (j in v) {
@@ -41,7 +42,8 @@ div#content.container_16.clearfix
p
a(href='#{rootPath}/model/#{model.collection.name}/document/new') New...
| &nbsp;|&nbsp;
- a(href='#{rootPath}/model/#{model.collection.name}') View All...
+ a(href='#{rootPath}/model/#{model.collection.name}')
+ span(id='viewAll_#{model.collection.name}') View All...
div.grid_5
h2 Recent Activity
p
View
17 lib/mongoose-admin.js
@@ -143,6 +143,23 @@ MongooseAdmin.prototype.getModel = function(collectionName, onReady) {
onReady(null, this.models[collectionName].model, this.models[collectionName].fields, this.models[collectionName].options);
};
+/**
+ * Get the counts of a model
+ *
+ * @param {String} collectionName
+ *
+ * @api public
+ */
+MongooseAdmin.prototype.modelCounts = function(collectionName, onReady) {
+ this.models[collectionName].model.count({}, function(err, count) {
+ if (err) {
+ console.log('Unable to get counts for model because: ' + err);
+ } else {
+ onReady(null, count);
+ }
+ });
+};
+
/**
* List a page of documents from a model
*
View
2 package.json
@@ -2,7 +2,7 @@
"name": "mongoose-admin"
, "description": "automatic admin tool and admin pages for mongoose"
, "keywords": ["mongoose", "mongo", "admin"]
- , "version": "0.0.2"
+ , "version": "0.0.3"
, "homepage": "http://www.github.com/marccampbell/mongoose-admin"
, "repository": "git://github.com/marccampbell/mongoose-admin.git"
, "author": "Marc Campbell <marc.e.campbell@gmail.com> (http://twitter.com/mccode)"

0 comments on commit 0b142d8

Please sign in to comment.
Something went wrong with that request. Please try again.