简体   繁体   English

在Backbone.js中触发嵌套模型事件

[英]Firing nested model events in Backbone.js

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) 我的关系是这样的: 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. 我的想法是使用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);
this.files = new Files ;

应该:

this.files = new Files() ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM