0

EDITED I'm trying destroy or set or another function use but my error is

Uncaught TypeError: this.model.destroy is not a function

Looks like there are no errors on code

my code

define(['jquery',
    'underscore',
    'backbone',
    'handlebars',
    '/resources/app/models/TravelModel.js',
    ], function ($, _, Backbone, Handlebars, Travel) {

     var addAdminPanelView = Backbone.View.extend({

            el:$(".page"),
            model:Travel,
            events:{
                'click #editButton':'editEvent',
            },

            deleteEvent:function(){

                this.model.destroy(),
                });

            },
curious_
  • 3
  • 5

1 Answers1

2

mikeapr4's assumption is correct.

Your Travel model is probably (and should be) something like this:

define(['Backbone'], function(Backbone) {
    return Backbone.Model({
        // ...
    });
});

So in your view, when your calling this.model.destroy(), it's equivalent to calling Travel.destroy() and it won't work since Travel is not an instance, but a constructor.

Backbone Collection do take a model constructor for its model property, but a Backbone View expects an instance.

You either need to initialize the view and create a Travel instance.

// Uppercase since it's a type, not an instance.
var AddAdminPanelView = Backbone.View.extend({
    el: $(".page"),
    events: {
        'click #editButton': 'editEvent',
    },

    // initialize the view with a Travel instance
    initialize: function() {
        this.model = new Travel();
    },

    deleteEvent: function() {
        this.model.destroy(),
    }
});

Or pass a Travel instance as an option on the view's construction.

var travelInstance = new Travel();

var view = new AddAdminPanelView({
    model: travelInstance
});
Community
  • 1
  • 1
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129