创建视图的主干视图

时间:2012-02-01 02:25:27

标签: javascript jquery backbone.js

所以这就是事情。我正在制作一个使用backboneJS的应用程序。我目前创建了不同行的列表。每行都有一个带有功能editRow的编辑按钮和一个带有功能removeRow的删除按钮。目前editRow工作,它创建新视图,在这种情况下是一个modalWindow及其app.EditRowView。这个视图有另一个我无法绑定的按钮。不受约束的函数是modalWindow。 有线索吗?我的逻辑错了吗?视图是否应该创建其他视图? 任何帮助都像往常一样受到赞赏。

    app.FormRowView = Backbone.View.extend({
        tagName:'li',
        events: {
            "click .danger"         : "removeRow",
            "click .primary"        : "editRow"

        },

        initialize:function(){
            var template =this.model.get('temp');
            _.bindAll(this, 'render');
            this.model.bind('change', this.render);
            this.template = _.template($("#"+template).html());
        },

        render:function(){
            var renderedContent = this.template(this.model.toJSON());

            $(this.el).html(renderedContent);

            $(function() {
              $( ".sortable" ).sortable();
              $( ".sortable" ).disableSelection();
            });


            return this;

        },

        removeRow:function(){
            $(this.el).remove();
        },

        editRow:function(){
            var view = new app.EditRowView({
                model:this.model,
                collection: this.collection
            });
            $("body").append(view.render().el);
            return this;
        }

    });

    app.EditRowView = Backbone.View.extend({

        events: {
            "click .save"       : "modalWindow"
        },

        initialize:function(){

            var template =this.model.get('editScreenTemplate');
            _.bindAll(this, 'render');
            this.model.bind('change', this.render);
            this.template = _.template($("#"+template).html());
        },

        render:function(){
            var renderedContent = this.template(this.model.toJSON());
            $(this.el).html(renderedContent);
            return this;

        },

        modalWindow:function(){
            alert("im in");

        }
    });


基本上发生的是函数modalWindow永远不会触发。我在UI中有正确的元素(带有.save类的按钮)

1 个答案:

答案 0 :(得分:0)

嘿伙计们,所以我把它搞定了。我需要指定一个tagName,以便委托事件绑定到该视图... 谢谢你的方式