Implementing a sortable Image collection doesn't work
<template name="gallery">
<div id="grid-container" class="cbp-l-grid-agency grid">
{{#each images getCurrentCategory}}
{{> image}}
{{/each}}
</div>
</template>
Template.gallery.created = function(){
Tracker.autorun(function () {
Meteor.subscribe('images'), self.limit.get()
});
}
Template.gallery.rendered = function(){
//// Image reordering /////
this.$('#Images').sortable({
stop: function(e, ui) {
el = ui.item.get(0)
before = ui.item.prev().get(0)
after = ui.item.next().get(0)
if(!before) {
newRank = Blaze.getData(after).rank - 1
} else if(!after) {
newRank = Blaze.getData(before).rank + 1
}
else
newRank = (Blaze.getData(after).rank +
Blaze.getData(before).rank)/2
Images.update({_id: Blaze.getData(el)._id}, {$set: {rank: newRank}})
}
})
}
Template.gallery.helpers({
'getCurrentCategory':
function() {
return Template.instance().currentcategory.get();
},
'category': function(){
var allImages = Images.find().fetch();
var categoryList = _.uniq(allImages, false, function(d) {return d.category});
var a = _.pluck(categoryList, "category");
return a;
},
'images': function (currentcategory) {
if(currentcategory == 'all' || !currentcategory){
return Images.find({},{sort: {rank: -1}});
}
return Images.find({category:currentcategory});
}
});
What else need to be added or to be modified in the code ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…