2012-04-01 8 views
7

sto ottenendo l'errore:Backbone - oggetto renda non ha un metodo 'applicare'

object render has no method apply for the below code.

Quale può essere il motivo? La pagina HTML non contiene alcun codice eccetto il collegamento per javascript.
Cosa devo fare per rimuovere l'errore?

(function($) { 

    window.Book = Backbone.Model.extend({}); 

    window.Library = Backbone.Collection.extend({ 

     model: Book 

    }); // end of Collection 
    window.LibraryView = Backbone.View.extend({ 

     el: $('body'), 

     events: { 
      'click button#btn_add': 'btn_add' 

     }, 

     initialize: function() { 
      $(this.el).append("View initialized"); 
      _.bindAll(this, 'render', 'btn_add'); 
      this.collections = new Library(); 
      this.collections.bind('add', 'render', this); 
      this.startingDisplay(); 

     }, 
     startingDisplay: function() { 
      $(this.el).append("<input type='text' id='t1' /><button id='btn_add'>Add</button>"); 

     }, 

     btn_add: function() { 

      book = new Book({ 
       title: "first" 
      }); 
      alert("Name : " + book.get('title')); 
      this.collections.add(book); 
     }, 

     render: function() { 
      alert("render called"); 

     }, 

    }); // end of LibraryView 
    libraryview = new LibraryView(); 

})(jQuery);​ 

risposta

4

Non si sta utilizzando la sintassi corretta per legare l'evento add collezione. Utilizzare:

// this.collections.bind('add', 'render', this); 
this.collections.bind('add', this.render, this); 

Il secondo parametro è previsto un callback (una funzione).

DEMO

+0

grazie mille, grande lavoro! – user1305989

+0

perché dovrei restituire "questo" nella funzione di rendering? Che cosa fa ? – user1305989

+0

Quindi è concatenabile, quindi puoi fare 'myView (someModel) .render(). El'. Nella vista principale potrebbe non essere così interessante, ma diventa utile quando si ha una vista per - diciamo - un oggetto ContactItem. –