无法删除模型的“视图”

时间:2013-05-17 09:22:24

标签: jquery backbone.js backbone-views backbone-model

在我看来,我有一个名为clear的click事件,可以从页面中移除模型和元素..在我的视图中使用它我有这样的功能:

define(["backbone","../router/router"], function(Backbone,router){

    "use strict"

    var appView = Backbone.View.extend({
        tagName:"li",
        template:_.template($("#listTemp").html()),
        events:{
            "click" : "clear"
        },
        initialize:function(){
            this.render();
        },
        render:function(){
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        },
        listTrigger:function(){
            var studentName = this.model.get("name");
            router.navigate("#/list/" + studentName);

        },
        clear:function(){
            this.model.clear(); // i am triggering models clear method here..
        }
    });

    return  appView;

})

在我的模型中我有方法并清除模型和元素..

define(["jquery","underscore","backbone"], function($,_,Backbone){

    "use strict"

    var appModel = Backbone.Model.extend({
        initialize:function(){
            console.log(this.view);
        },
        clear:function(){
            this.destroy(); //it works..
            this.view.remove(); // but it throw the error;
        }
    });

    return appModel;

});

我得到的错误是:

Uncaught TypeError: Cannot call method 'remove' of undefined model.js:11
Backbone.Model.extend.clear model.js:11
Backbone.View.extend.clear appView.js:24
b.event.dispatch jquery.min.js:3
v.handle

为什么这个'未定义' - 我来到这里..如何解决它...任何人都可以帮助我......?

1 个答案:

答案 0 :(得分:0)

我在appView中启动了我的观点 -

    initialize:function(){
                this.render();
                this.model.view = this;
            },
    remove:function(){
            $(this.el).remove(); //removing element
        },

工作正常。谢谢大家。