我正在尝试构建一个注册列表,因此人们可以注册,重新排列和删除。这是我遇到的问题;我想从MongoDb集合中选择前两个项目(键)。我该如何做到这一点,以便下一个在线的人显示在一个新模板中。我知道发现可能有所帮助,但我真的不确定我到底要搜索什么。是否更容易限制#each循环中的所有内容?
这是我的代码:
服务器端:
Meteor.publish( 'artists', function() {
return Artists.find( {}, { sort: { order: 1 } } );
});
客户端:
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import { Session } from 'meteor/session'
import './main.html';
var $ = require('jquery');
require('fancybox')($);
let initSortable = ( sortableClass ) => {
let sortableList = $( sortableClass );
sortableList.sortable( 'destroy' );
sortableList.sortable();
sortableList.sortable().off( 'sortupdate' );
sortableList.sortable().on( 'sortupdate', () => {
updateIndexes( '.sortable' );
});
};
let updateIndexes = ( sortableClass ) => {
let items = [];
$( `${sortableClass} li` ).each( ( index, element ) => {
items.push( { _id: $( element ).data( 'id' ), order: index + 1 } );
});
Meteor.call( 'updateArtistOrder', items, ( error ) => {
if ( error ) {
console.log( error.reason );
}
});
};
Template.artists.onCreated( () => {
let template = Template.instance();
template.subscribe( 'artists', () => {
initSortable( '.sortable' );
});
});
Template.artists.events({
'submit #add-artist' ( event, template ) {
event.preventDefault();
let artist = {
artistName: template.find( '[name="artistName"]' ).value
};
Meteor.call( 'addArtist', artist, ( error ) => {
if ( error ) {
console.log( error );
} else {
$( event.target ).get(0).reset();
$( '[name="artistName"]' ).focus();
initSortable( '.sortable' );
}
});
},
'click .delete-artist' ( event, template ) {
if ( confirm( 'Are you sure? This is permanent!' ) ) {
Meteor.call( 'deleteArtist', this._id, ( error ) => {
if ( error ) {
console.log( error );
} else {
updateIndexes( '.sortable' );
}
});
}
}
});
Template.artists.helpers({
artists() {
return Artists.find();
}
});
收集:
Artists = new Mongo.Collection( 'artists' );
Artists.allow({
insert() { return false; },
update() { return false; },
remove() { return false; }
});
Artists.deny({
insert() { return true; },
update() { return true; },
remove() { return true; }
});
模板:
<template name="artists">
<div class="container">
<h3 class="page-header">Next Up</h3>
<ul class="list-group sortable">
{{#each artists}}
<li data-id="{{_id}}" class="list-group-item clearfix">
<span class="pull-left">{{order}}. {{artistName}}</span>
<i class="fa fa-remove pull-right text-danger delete-artist"></i>
</li>
{{/each}}
</ul>
<form id="add-artist">
<div class="row">
<div class="col-xs-12 col-sm-5">
<label for="artistName">Register Here:</label>
<input type="text" name="artistName" class="form-control" placeholder="Name">
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-2">
<button type="submit" class="btn btn-success btn-block">Add Name</button>
</div>
</div>
</form>
</div>
</template>
答案 0 :(得分:1)
实现此目的有两种方法:
答案 1 :(得分:1)
尝试在客户端帮助程序中执行此操作:
Template.artists.helpers({
artists() {
return Artists.find({}, { limit: 2, sort: {order: 1} });
}
});
这样,只会从你的mongodb集合中检索到两个对象。