Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8634559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:45:20+00:00 2026-06-12T09:45:20+00:00

Problem: When I’m trying to fire any child event (Files), the parent’s event is

  • 0

Problem: When I’m trying to fire any child event (Files), the parent’s event is fired with it (Files).

My relation is like this: Project ---> (Many)Files(If is only folder) ---> (Many)Files and so on(If is only folder)

My idea is to construct folder tree using Backbone.Js.

My code as follows:

(function ($){

//Models ********************************************************************

window.Project = Backbone.Model.extend({

    defaults : {
      state : "collapsed"
    },

    initialize : function(){
        this.files = new Files ;
        //this.files.url = "rest/operations/files/0.json";
        this.files.url = this.get("project_id")+"/rest/operations/files/0.json";
        //this.files.fetch();
    } , 

    isCollapsed : function (){
        return (this.get("state") === "collapsed");
    },

    isExpanded : function(){
        return (!this.isCollapsed());
    },

    collapse : function(){
        this.set({
            'state' : 'collapsed'
        });
    },

    expand : function(){
        this.set({
            'state' : 'expanded'
        });
    }

});


window.File = Backbone.Model.extend({

    defaults : {
      state : "collapsed" ,
    },

    initialize : function(){
        //if(this.isItHasChild() && this.isItFolder()){  //check the child from the server
        if(this.isItFolder()){
            //means has files as child
            this.files = new Files ; 
            //this.files.url = "rest/operations/files/"+this.get("file_id")+".json";
            //this.files.url = this.get("project_id")+"/rest/operations/files/"+this.get("file_id")+".json";

            //test
            this.files.url = this.get("project_id")+"/rest/operations/files/0.json";
            //this.set({"modelCid" : this.cid});
        }    
    }, 

    isItHasChild : function (){
        return (this.get("isHasChild"));
    },

    isItFolder : function(){
        return (this.get("is_folder"));
    },

    isCollapsed : function (){
        return (this.get("state") === "collapsed");
    },

    isExpanded : function(){
        return (!this.isCollapsed());
    },

    collapse : function(){
        this.set({
            'state' : 'collapsed'
        });
    },

    expand : function(){
        this.set({
            'state' : 'expanded'
        });
    }

});


//Collections *************************************************************

window.Projects = Backbone.Collection.extend({
    //find a way to load the project number
    model : Project,
    url : "1/rest/operations/projects.json"
});


window.Files = Backbone.Collection.extend({
    model : File
});


//Views **********************************************************************

$(document).ready(function(){

    window.ProjectView = Backbone.View.extend({

        template : _.template($("#project-template").html()),

        initialize : function(){
            _.bindAll(this,'render','expand','collapse','renderFile','btn_clicked');
            //this.template = _.template($("#project-template").html());
            this.model.files.bind('reset',this.render);
        } , 

        events : {
            'click .project_btn_expand_collapse' : "btn_clicked"
        } , 

        render : function(){
            //console.log(this.model.cid);
            $(this.el).html(this.template(this.model.toJSON()));
            this.model.files.each(this.renderFile);
            return this ;
        } , 

        btn_clicked : function(){
            //alert(this.model.cid+" "+this.model.get('state'));
            if(this.model.isCollapsed()){
                this.expand();
            }else {
                //this.collapse();
            }
        },

        expand : function (){
            this.model.files.fetch();
            this.updateState();
        } , 

        collapse : function (){
          this.model.files.reset();
          this.updateState();
        },

        updateState : function(){
            if(this.model.isCollapsed()){
                this.model.expand();
            } else {
                this.model.collapse();
            }
        },

        renderFile : function (file){
            var view = new FileView({
                model : file , 
                id : file.cid , 
            });

            this.$("ul").append(view.render().el);
        }

    });


    window.FileView = Backbone.View.extend({

        template : _.template($("#file-template").html()),
        tagName : 'li',

        events : {
            "click" : "btn_clicked"
        },

        initialize : function(){
            _.bindAll(this,'render','expand','collapse','renderFile','btn_clicked');
            console.log(this.model.cid);

            //if(this.model.isItHasChild() && this.model.isItFolder()){ //as what we have said before we have to check the server
            if(this.model.isItFolder()){
                this.model.files.bind('reset',this.render);
            }  
        },

        render : function(){
            //console.log(this.model.cid);
            $(this.el).html(this.template(this.model.toJSON()));
            this.model.files.each(this.renderFile);
            return this ;
        },

        btn_clicked : function(){
            alert(this.model.cid+" "+this.model.get('state'));
            if(this.model.isCollapsed()){
                this.expand();
            }else {
                //this.collapse();
            }
        },

        renderFile : function (file){
            var view = new FileView({
                model : file , 
                id : file.cid , 
            });
            $(this.el).append("<ul></ul>");
            this.$("ul").append(view.render().el);
        },

        expand : function (){
            this.model.files.fetch();
            this.updateState();
        }, 

        collapse : function (){
          this.model.files.reset();
          this.updateState();
        },

        updateState : function(){
            if(this.model.isCollapsed()){
                this.model.expand();
            } else {
                this.model.collapse();
            }
        }

    });


    //run the application *********************************************************************************

    window.IDEHTML5 = Backbone.Router.extend({

        routes : {
            "" : "index", 
            ":id" : "getProjectID"
        },    

        initialize: function(){
        },

        getProjectID : function(id){
            alert(id);
        },

        index : function(){
            var projects = new Projects();
            projects.fetch({
                success : function(){
                    var project = projects.at(0);
                    this.project_view = new ProjectView({model : project});
                    $("#container").html(this.project_view.render().el);
                },
                error : function(){ }
            }); 
        }

    });


    //run the application

    window.App = new IDEHTML5();
    //Backbone.history.start({pushState: true});
    Backbone.history.start();

});


})(jQuery);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:45:21+00:00Added an answer on June 12, 2026 at 9:45 am
    this.files = new Files ;
    

    Should be:

    this.files = new Files() ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem is like this: Suppose there are records in dataTable with FruitType as A
Problem is like this: I have two winapi application's. There is only one way
Problem: I have a table that prints out vertical but I would like it
Problem: I am trying to build a recursive tree using a function and data
Problem: I need an array (datamember) in a parent class A. However, depending on
PROBLEM: Here's the code: import matplotlib.pyplot as plt plt.bar([1,2],[5,4]) plt.title('this is a very long
I am trying to render a haml file in a javascript response like so:
Problem Statement : Execute Various Command randomly by matching its percentage. like execute CommandA
Problem I'm currently stuck trying to figure out what iPhone OS Deployment Target setting
Problem in short: I'm trying to make a simple show/hide toggle work on just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.