Answer the question
In order to leave comments, you need to log in
Meteor.js: Why are elements duplicated?
Good afternoon,
The problem worries - hands are already lowered. I am creating a project on Meteor.js. I am using the nestedSortable library to create a nested list with drag`n`drop elements. When the list structure is changed, the sort field in the database is updated in JSON format, like [{"id":"1417619247009","children":[{"id":"1417619241424"}]}].
After, from which the list is restored in the desired sorting, using a recursive function that works and returns the correct result.
The most interesting thing is that when I use the database and pull out the task from the nested list one level below, it is simply duplicated, which is rather strange, because the correct information goes into the template. Here is an example of a duplication:
The most interesting thing is that if you remove the sorting update in the database, then everything works correctly)
I will give a little code to make it clearer))
Template.item.helpers({
tasklist: function () {
if (Meteor.user()) {
var task = null,
result = [],
sorting = null;
if (Meteor.user().sort) {
task = Task.find({user_id: Meteor.userId()}).fetch()
sorting = JSON.parse(Meteor.user().sort);
sorting.forEach(function(current_item) {
//Рекурсивная функция здесь
var nested = taskFilter (current_item, task);
result.push (nested);
});
}
else {
return task;
}
return result;
}
}
});
function taskFilter (item, all) {
var result = [];
$.map(all, function (task) {
if (task.id == item.id) {
if (item.children) {
task.children = []
item.children.forEach(function(child) {
var children = taskFilter (child, all);
task.children.push (children);
//console.log (result);
});
}
return result = task;
}
});
return result;
}
<template name="item">
{{#each tasklist}}
{{>taskitem}}
{{/each}}
</template>
<template name="taskitem">
<li class="task-item">
....
{{#if children}}
<ul>
{{#each children}}
{{>taskitem}}
{{/each}}
</ul>
{{/if}}
</li>
</template>
Answer the question
In order to leave comments, you need to log in
And if you turn on the update in the database, and then sort it, see what is duplicated, and refresh the page, everything will be in its place, as it should be? If so, do you use subscribe/publish? if yes, then maybe there is an under-scheduling with the local mongo storage and the server one?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question