PROBLEM: I have table with tr that have data from table, When I insert element in Mongo and post it on my page, element added on the bottom of the table
Posts = new Mongo.Collection('posts');
Meteor.publish('allPosts', function(){
return Posts.find({}, { sort: { date: -1 }} );
});
Meteor.subscribe("allPosts");
Posts.insert({
date: 1
})
Posts.insert({
date: 2
})
Posts.insert({
date: 3
})
<table class=" table">
{{#each posts}}
{{> postJobs}}
{{/each}}
</table>
<template name="postJobs">
<tr class="smallInfo">
<td>{{date}}</td>
</tr>
</template>
In DOM I have:
<table>
<tr>
<td>1</td> // must be 3
</tr>
<tr>
<td>2</td> // must be 2
</tr>
<tr>
<td>3</td> // must be 1
</tr>
</table>
All my last inserts must add to the top of my page (table)
EDIT : My problem with dynamic insert in collection (and i know about -1 gramatik mistake) EXAMPLE :
Meteor.startup(function(){
Meteor.setTimeout(function(){Test("10");}, 1000);
function Test(x)
{
Posts.insert( {
submitDate: moment().format()
});
}
Meteor.setTimeout(function(){Test("10");}, 10000);
If i sort by submitDate it will show like :
<tr><td>10-10-10</td></tr> // must be 10-10-20
<tr><td>10-10-20</td></tr> // must be 10-10-10
BUT when i refresh my page(F5) all ok
<tr><td>10-10-20</td></tr>
<tr><td>10-10-10</td></tr>
Why do you not sort descending?
{sort: { date: -1 } }
Cheers, Tom
UPDATE:
You can find a live example at a MeteorPad I prepared:
http://meteorpad.com/pad/Ba5DTe94NjFi3ZTPA/Playground_Flow-Router_Chat
You have to do the sort on the client side not within the publish method.
This is why you get first time sorted but then just as inserted returns.
If you do the sort on client side find()
the minimongo will do it on each new document.
Hope this helps for you. Tom
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments