模型销毁返回undefined不是一个函数

时间:2014-11-25 03:16:17

标签: jquery backbone.js backbone-views

我正在尝试销毁模型,当我调用this.model.destroy({options})

时,我得到“Uncaught TypeError:undefined is not function”

我的模型是这样的:

    window.CompanyModel = Backbone.Model.extend({

        initialize: function() {
            console.log('CompanyModel:initialized');
        },

        parse: function(response, options) {
            return response;
        },

    });

收集:

    window.CompaniesCollection = Backbone.Collection.extend({

        model: window.CompanyModel,

        initialize: function() {
            console.log('CompaniesCollection:initialized');
        },  
    });

查看:

    window.CompaniesView = Backbone.View.extend({
         initialize : function() {
             console.log('CompaniesView:initialized');
             this.companies = new CompaniesCollection();
             this.companies.url = function(id) {
                 return '../src/rest/administration/companies/companies';
             };
             this.company = new CompanyModel();
         },
         [...more code...]
         confirmDeleteForm : function(event) {
             event.preventDefault();

             var company_id = $('#company_existing').val();
             var self=this;
             this.company = this.companies.where({'company_id':company_id});

             this.company.destroy({
                 wait: true,
                 success: function(model, response) {
                     console.log('deleted');
                     $('#deleteModal').modal('hide');
                     self.cleanForm();
                     self.getCompanies();
                 },
                 error: function(model, response) {
                     console.log('error');
                     console.log(model);
                     console.log(response);
                     console.log(options);
                     if (response.status == 500) {
                        self.showErrorMessage('Se produjo un error en el servidor', 'ERROR');
                     } else {
                        self.showErrorMessage(response.responseText, 'ATENCION');
                     }
                }
           });

       },

任何帮助都将不胜感激。

REgards,LN

1 个答案:

答案 0 :(得分:4)

this.companies.wherefine manual

返回一个数组
  

其中 collection.where(attributes)

     

返回集合中与传递的属性匹配的所有模型的数组。

这意味着:

this.company = this.companies.where({'company_id':company_id});

this.company中留下一个数组,而数组没有destroy个方法。

如果this.companies中只有一个与{company_id: company_id}匹配的条目,那么您可以说:

this.company = this.companies.where({'company_id':company_id})[0];
// -----------------------------------------------------------^^^

或者您可以使用findWhere

  

findWhere collection.findWhere(attributes)

     

就像 where 一样,但只直接返回集合中与传递的属性相匹配的第一个模型

并说:

this.company = this.companies.findWhere({'company_id':company_id});

如果是这种情况,那么您的company_id属性可能确实是公司的唯一标识符,那么您可能希望将模型中的idAttribute设置为company_id

window.CompanyModel = Backbone.Model.extend({
    idAttribute: 'company_id',
    //...

这样您就可以使用Collection#get无需搜索即可找到它。