2012-04-01 14 views
7

मैं त्रुटि हो रही है:बैकबोन - वस्तु प्रस्तुत करना कोई विधि 'लागू करें' है

object render has no method apply for the below code.

क्या कारण हो सकता है? एचटीएमएल पेज में जावास्क्रिप्ट के लिंक को छोड़कर कोई भी कोड नहीं है।
त्रुटि को हटाने के लिए मुझे क्या करना चाहिए?

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

उत्तर

4

आप सही सिंटैक्स का उपयोग नहीं कर रहे हैं add संग्रह घटना बाध्य करने के लिए। उपयोग करें:

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

दूसरा पैरामीटर कॉलबैक (एक फ़ंक्शन) होने की उम्मीद है।

DEMO

+0

बहुत बहुत शुक्रिया, महान काम कर रहा! – user1305989

+0

मुझे रेंडर फ़ंक्शन में "यह" क्यों वापस करना चाहिए? यह क्या करता है ? – user1305989

+0

तो यह श्रृंखला योग्य है, इस प्रकार आप 'myView (someModel) .render() el' कर सकते हैं। मुख्य दृश्य में यह दिलचस्प नहीं हो सकता है लेकिन जब आपके पास कोई विचार है तो यह आसान हो जाता है - मान लें - एक संपर्क Item। –

संबंधित मुद्दे