0

在我看来,我有一个名为 clear 的单击事件,用于从页面中删除模型和元素。要在我的视图中使用它,我有这样的功能:

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

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

4

1 回答 1

0

我在我的 appView 中启动了我的视图 -

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

工作正常。谢谢大家。

于 2013-05-17T09:57:34.900 回答