2017-04-24 48 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'屬性。 –