2013-11-23 89 views
0

我有這個簡單的BackboneJS應用程序,並且它在向集合添加新模型時不斷返回此錯誤:無法調用未定義的方法'on'。有人能幫我嗎。我在這裏看不到問題。我在index.html中定義了我的模板,並且使用了Slim框架和NotORM。BackboneJS - 無法調用未定義的方法'on'

(function(){ 

    window.App = 
    { 
     Models:{}, 
     Collections: {}, 
     Views : {} 
    } 

    window.template = function(id) 
    { 
    return _.template(jQuery('#' + id).html()); 
    } 

    App.Models.Party = Backbone.Model.extend({ 


    }); 

    App.Collections.Partys = Backbone.Collection.extend({ 

     model: App.Models.Party, 
     url: "http://localhost/BackboneJS/vjezba6/server/index.php/task" 
    }); 

    App.Views.Party = Backbone.View.extend({ 

     tagName :"div", 
     className: "box shadow aktivan", 
     template: template("boxovi"), 

     initialize: function() 
     { 
      this.model.on('change', this.render, this); 
     }, 

     events:{ 

      "click .izbrisi" : "izbrisi" 
     }, 

     render: function() 
     { 
     var template = this.template(this.model.toJSON()); 

     this.$el.html(template); 

     return this; 
     }, 

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

     ukloni: function() 
     { 
      this.remove(); 
     } 



    }); 

    App.Views.Partys = Backbone.View.extend({ 


     tagName:"div", 
     id: "nosac-boxova", 

     initialize: function() 
     { 

     }, 

    render: function() { 
     this.collection.each(this.addOne, this); 

     return this; 
    }, 

    addOne: function(party) { 
     var partyView = new App.Views.Party({ model: party }); 
     this.$el.append(partyView.render().el); 
    } 

    }); 

    App.Views.Dodaj = Backbone.View.extend({ 

     tagName: "div", 
     template : template("dodajTemp"), 
     events: 
     { 
      'submit' : 'submit' 
     }, 

     submit : function(e) 
     { 
      e.preventDefault(); 
      console.log(e); 
      var nazivPolje = $(e.currentTarget).find(".naziv").val(); 
      var tekstPolje = $(e.currentTarget).find(".lokal").val(); 

      var noviParty = new App.Views.Party({naziv:nazivPolje, tekst: tekstPolje}); 
      this.collection.create(noviParty); 
     }, 

     initialize: function() 
     { 

     }, 

     render: function() 
     { 
      var template = this.template(); 
      this.$el.html(template); 
      return this; 
     } 

    }); 


/* var kolekcijaPartya = new App.Collections.Partys([ 

     { 
     naziv:"Ovo je prvi naziv", 
     tekst: "Ovo je prvi tekst" 
     }, 
     { 
     naziv:"Ovo je drugi naziv", 
     tekst: "Ovo je drugi tekst" 
     } 

    ]);*/ 

    var kolekcijaPartya = new App.Collections.Partys; 
    kolekcijaPartya.fetch({ 

    success: function() 
    { 

     var partysView = new App.Views.Partys({collection:kolekcijaPartya}); 
     $("#content").prepend(partysView.render().el); 
     $("div.box").slideAj(); 


     var dodajView = new App.Views.Dodaj({collection: kolekcijaPartya}); 
     $("div#sidebar-right").html(dodajView.render().el); 
    } 




    }); 




})(); 

回答

1
var noviParty = new App.Views.Party({naziv:nazivPolje, tekst: tekstPolje}); 
this.collection.create(noviParty); 

所以你嘗試將視圖添加到您的收藏?

+0

謝謝你好時間。愚蠢的錯誤,我永遠不會找到。 – Sysrq147

相關問題