S
S
slavagoreev2014-12-03 18:23:19
meteor
slavagoreev, 2014-12-03 18:23:19

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:
f3143e977b7843d7a83cd0ee5c598b33.gif
The most interesting thing is that if you remove the sorting update in the database, then everything works correctly)
e43e9f930b114e3dbf849a3bd199ac56.gif
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>

I hope for your help.
PS possible for a small fee, for example, 500 rubles. Write here or email [email protected]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Drozdov, 2014-12-03
@bagzon

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?

_
_ _, 2014-12-03
@AMar4enko

That's why I just can't force myself to do a meteor - how to look for answers to such questions? Live on their forums?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question