2017-04-24 49 views
0

我试图破坏或设置或其他功能使用,但我的错误是this.model没有编辑的功能

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

看起来有对码

我的代码没有错误

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(), 
       }); 

      }, 
+0

'$(本).model.destroy()'或'el.model.destroy(),' –

+0

“遗漏的类型错误:无法读取属性 '破坏' 的未定义” @AlivetoDie – pinarkoroglu

+0

'$(这个).model.remove(),''或'el.model.remove(),' –

回答

2

mikeapr4's assumption是正确的。

Travel模型可能(也应该)是这样的:

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

所以,当你调用this.model.destroy(),这相当于调用Travel.destroy(),它不会工作,因为Travel不一个实例,但是一个构造函数。

Backbone Collection确实为其model property建模,但Backbone View需要一个实例。

您或者需要初始化视图并创建一个Travel实例。

// 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(), 
    } 
}); 

或通过Travel实例作为视图构造的选项。

var travelInstance = new Travel(); 

var view = new AddAdminPanelView({ 
    model: travelInstance 
}); 
+0

谢谢你的工作。所以我可以问你一些关于这个问题的东西吗? – pinarkoroglu

+0

如何获取表格数据的ID?我试过这个。$ el.find(“。location”).data(“id”)但是控制台打印“undefined” – pinarkoroglu

+0

@ marshall12语法看起来不错,所以可能''.location'不存在于视图中,或者它没有元素上的'data-id'属性。 –