0

In my view, i have a click event called clear to remove both model, and element from the page.. to use this in my view i have the function like this:

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;

})

in my model i have the method and clearing the modle and element..

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;

});

the error i am getting is:

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

why this 'undefined' - i am getting here.. how to solve it.. can any one help me..?

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

1 Answers1

0

I initiated my view in my appView -

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

works fine. thanks for all.

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247