Pergunta

I am working on a Meteorjs application where I have a syllabus and I want to copy or clone this syllabus. Here in my syllabus there are modules which can till multiple levels means a mudule can have another module as its child module and so on.for example i have a module Grammer and grammer has another module named tenses and tenses ahs some other module named present past and future these will seems like this.

tenses
    past
        simple
        negative
        interogative
    present
        simple
        negative
        interogative
    future
        simple
        negative
        interogative

databse fields are name, parent_id, has_children, syllabus_id these mudules can further have some sub modules or children. In database to check if a module have further children i have a field boolean has_children and parent_id to relate the child with the parent.If element is a root element its 'parent_id` will be null.

for this i tried to reach every element recursively but i am able to reach only one element at one level. means i am not reaching to the siblings of an element but the children.

My code is :

cloneSyllabus:function(syllabi_id){
    syll_obj=Meteor.syllabi.findOne({_id:syllabi_id});
    new_syllabi_id=Meteor.syllabi.insert({

                                        subject_id:syll_obj.subject_id,
                                        level_id:syll_obj.level_id,
                                    });
    var parent_objs = Meteor.collecton.find({
                                              parent_id:null, 
                                              syllabus_id:syllabi_id}).fetch()

    for(var i=0;i<syll_design_objs.length;i++){
        makeClone(parent[i],null,new_syllabi_id)    
    }
}

and the other function which is creating the objects recursively is

function makeClone(item,parent_id,syllabi_id){
    var item_id=item._id
    delete item._id
    item.parent_id=parent_id
    item.syllabus_id=syllabi_id
    var new_parent_id=Meteor.syllabi_design.insert(item)
    if(item.has_children){
        child_items=Meteor.collection.find({parent_id:item_id}).fetch();
        for(i=0;i<child_items.length;i++){
            makeClone(child_items[i],item_id,syllabi_id);
        }
    }
}

But using this i am able to retrieve only one element at the same level but not its siblings. i will be able to create tenses past simple negative interogative tell me guys where i am making a mistake while retrieving these objects.Or tell me any otherway to accomplish the task. Here i have to change only the syllabus_id and create all the objects again.

Foi útil?

Solução

Everything is perfect in your code just add recursive function termination statement in your code. cloneSyllabus:function(syllabi_id){ syll_obj=Meteor.syllabi.findOne({_id:syllabi_id}); new_syllabi_id=Meteor.syllabi.insert({

                                    subject_id:syll_obj.subject_id,
                                    level_id:syll_obj.level_id,
                                });
var parent_objs = Meteor.collecton.find({
                                          parent_id:null, 
                                          syllabus_id:syllabi_id}).fetch()

for(var i=0;i<syll_design_objs.length;i++){
    makeClone(parent[i],null,new_syllabi_id)    
}
return 
}

And declare the variable i as var i and child_objs as var child_objs in loop of the function makeClone because these variable are being treated here as a glabal variable in your code.

function makeClone(item,parent_id,syllabi_id){
    var item_id=item._id
    delete item._id
    item.parent_id=parent_id
    item.syllabus_id=syllabi_id
    var new_parent_id=Meteor.syllabi_design.insert(item)
    if(item.has_children){
        child_items=Meteor.collection.find({parent_id:item_id}).fetch();
        for(var j=0;j<child_items.length;j++){
            makeClone(child_items[i],item_id,syllabi_id);
        }
    }
    else{
        return
    }
    }     

Hope this will work perfect for you

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top