Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection count performance #378

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
4 changes: 3 additions & 1 deletion lib/both/router.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
@AdminController = RouteController.extend
layoutTemplate: 'AdminLayout'
waitOn: ->
[
subs = [
Meteor.subscribe 'adminUsers'
Meteor.subscribe 'adminUser'
Meteor.subscribe 'adminCollectionsCount'
]
if AdminConfig?.waitOn? then subs = subs.concat(AdminConfig?.waitOn.call(this))
subs
onBeforeAction: ->
Session.set 'adminSuccess', null
Session.set 'adminError', null
Expand Down
13 changes: 8 additions & 5 deletions lib/both/startup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ adminCreateTables = (collections) ->
if column.template
createdCell = (node, cellData, rowData) ->
$(node).html ''
Blaze.renderWithData(Template[column.template], {value: cellData, doc: rowData}, node)
Blaze.renderWithData(Template[column.template], {
value: cellData, doc: rowData, column: column}
, node)

data: column.name
title: column.label
Expand Down Expand Up @@ -125,8 +127,9 @@ adminCreateRouteEditOptions = (collection, collectionName) ->
template: "AdminDashboardEdit"
controller: "AdminController"
waitOn: ->
Meteor.subscribe 'adminCollectionDoc', collectionName, parseID(@params._id)
collection.routes?.edit?.waitOn
handle = Meteor.subscribe 'adminCollectionDoc', collectionName, parseID(@params._id)
handles = collection.routes?.edit?.waitOn?.apply(@, arguments) ? []
_.union([handle], handles)
action: ->
@render()
onAfterAction: ->
Expand All @@ -136,7 +139,7 @@ adminCreateRouteEditOptions = (collection, collectionName) ->
Session.set 'admin_collection_name', collectionName
Session.set 'admin_id', parseID(@params._id)
Session.set 'admin_doc', adminCollectionObject(collectionName).findOne _id : parseID(@params._id)
collection.routes?.edit?.onAfterAction
collection.routes?.edit?.onAfterAction?.apply(@, arguments)
data: ->
admin_collection: adminCollectionObject collectionName
_.defaults options, collection.routes?.edit
Expand Down Expand Up @@ -195,7 +198,7 @@ Meteor.startup ->
data: 'emails'
title: 'Email'
render: (value) ->
value[0].address
value?[0]?.address
searchable: true
}
{
Expand Down
10 changes: 6 additions & 4 deletions lib/client/html/admin_templates.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<template name="AdminDashboard">
{{#each adminWidgets}}
{{> UI.dynamic template=template data=data}}
{{#if isDefined adminWidgets}}
{{#each adminWidgets}}
{{> UI.dynamic template=template data=data}}
{{/each}}
{{else}}
{{> adminDefaultWidgets}}
{{/each}}
{{> adminDefaultWidgets}}
{{/if}}
</template>

<template name="AdminDashboardNew">
Expand Down
9 changes: 7 additions & 2 deletions lib/client/js/templates.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Template.AdminDashboardViewWrapper.destroyed = ->
Blaze.remove @data.view

Template.AdminDashboardView.rendered = ->
table = @$('.dataTable').DataTable();
table = @$('.dataTable').DataTable()
filter = @$('.dataTables_filter')
length = @$('.dataTables_length')

Expand Down Expand Up @@ -46,10 +46,15 @@ Template.AdminDashboardView.rendered = ->

Template.AdminDashboardView.helpers
hasDocuments: ->
AdminCollectionsCount.findOne({collection: Session.get 'admin_collection_name'})?.count > 0
count = AdminCollectionsCount.findOne({collection: Session.get 'admin_collection_name'})
# Default to true to iterate over docs if count is not stored.
if count? then count.count > 0 else true
newPath: ->
Router.path 'adminDashboard' + Session.get('admin_collection_name') + 'New'

Template.adminEditBtn.helpers
path: ->
Router.path "adminDashboard" + Session.get('admin_collection_name') + "Edit", _id: @_id

Template.AdminDashboard.helpers
isDefined: (obj) -> obj?
25 changes: 12 additions & 13 deletions lib/server/publish.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,26 @@ Meteor.publish 'adminUser', ->

Meteor.publish 'adminCollectionsCount', ->
handles = []
hookHandles = []
self = @

_.each AdminTables, (table, name) ->
id = new Mongo.ObjectID
count = 0
table = AdminTables[name]
ready = false
selector = if table.selector then table.selector(self.userId) else {}
handles.push table.collection.find().observeChanges
added: ->
count += 1
ready and self.changed 'adminCollectionsCount', id, {count: count}
removed: ->
count -= 1
ready and self.changed 'adminCollectionsCount', id, {count: count}
ready = true

docCollection = adminCollectionObject(name)
count = docCollection.find().count()
self.added 'adminCollectionsCount', id, {collection: name, count: count}

update = -> self.changed 'adminCollectionsCount', id, {count: count}
hookHandles.push docCollection.after.insert ->
count++
update()
hookHandles.push docCollection.after.remove ->
count--
update()

self.onStop ->
_.each handles, (handle) -> handle.stop()
_.each hookHandles, (handle) -> handle.remove()
self.ready()

Meteor.publish null, ->
Expand Down
5 changes: 3 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ Package.on_use(function(api){
'raix:[email protected]',
'reywood:[email protected]',
'momentjs:[email protected]',
'aldeed:tabular@1.4.0',
'aldeed:tabular@1.5.5',
'meteorhacks:[email protected]',
'zimme:[email protected]',
'mfactory:[email protected]'
'mfactory:[email protected]',
'matb33:[email protected]'
],
both);

Expand Down