【问题标题】:BackboneJS - Cannot call method 'on' of undefinedBackboneJS - 无法调用未定义的“on”方法
【发布时间】:2013-12-08 10:55:04
【问题描述】:

我有这个简单的 BackboneJS 应用程序,它在将新模型添加到集合时不断返回此错误:无法调用未定义的“on”方法。有人能帮我吗。我在这里看不到问题。我在 index.html 中定义了我的模板,并且我正在使用 Slim 框架和 NotORM。

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




   });




})();

【问题讨论】:

    标签: backbone.js methods call undefined


    【解决方案1】:
    var noviParty = new App.Views.Party({naziv:nazivPolje, tekst: tekstPolje});
    this.collection.create(noviParty);
    

    所以您正在尝试将视图添加到您的收藏中?

    【讨论】:

    • 谢谢你的好人。我永远找不到的愚蠢错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2012-11-12
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多