2012-06-08 32 views
18

Tôi đang cố gắng tải danh sách quốc gia và chọn quốc gia tôi chỉ muốn gọi cảnh báo, nhưng bằng cách nào đó tôi không thể bắt sự kiện thay đổi cho hộp chọn.Làm thế nào để ràng buộc sự kiện thay đổi trong Backbone?

Ai đó có thể giúp tôi không?

đây là mã với quan điểm, mô hình và một phần bộ sưu tập với mẫu kịch bản trong html

<!DOCTYPE html> 
<html> 
<head> 
    <title>Backbone</title> 
</head> 
<body> 
    <script type="text/template" id="country_select_template"> 
     <select id="country-selector"> 
      <% _.each(countries, function(country) {%> 
       <option value="<%= country.fipsCode %>"><%= country.countryName %></option> 
      <% }); %> 
     </select> 
    </script> 
    <div id="country_select_container"> 
     Loading country... 
    </div> 

    <!--div> 
    <select id="state" disabled=true> 
     <option value="NAN">Select State</option>      
    </select> 
    </div> 
    <div> 
    <select id="city" disabled=true> 
     <option value="NAN">Select City</option>      
    </select> 
    </div--> 
</div> 
<ul id="names-list"> 
</ul> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script> 
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script> 
<script> 

Country = Backbone.Model.extend(); 

CountryList = Backbone.Collection.extend({ 
    model : Country, 
    url : "https://dl.dropbox.com/u/18190667/countryList.json", 
    parse : function(response) { 
     return response.geonames; 
    } 
}); 

Countries = new CountryList(); 

CountryView = Backbone.View.extend({ 
    events: { 
     "change #country-selector": "countrySelected" 
    }, 

    countrySelected: function(){ 
     alert("You can procceed!!!"); 
    }, 

    countrySelected :function(){ 
     alert("Procceed"); 
    }, 

    initialize: function(){ 
     var countries = []; 
     var self = this; 
     Countries.fetch({ 
      success: function(){ 
       self.render(); 
      } 
     }); 
    }, 

    render: function(){ 
     var self = this; 
     var country_select_template = _.template($("#country_select_template").html(), { 
      countries: Countries.toJSON(), 
     }); 
     $('#country_select_container').html(country_select_template); 
     return this; 
    } 
}); 
var view = new CountryView(); 
</script> 

</body> 
</html> 

Trả lời

33

Hãy thử như thế này:

CountryView = Backbone.View.extend({ 
    el: $("#country_select_container"), 
    template: _.template($("#country_select_template").html()), 
    events: { 
     "change #country-selector": "countrySelected" 
    }, 

    countrySelected: function(){ 
     alert("You can procceed!!!"); 
    }, 

    initialize: function(){ 
     var self = this; 
     Countries.fetch({ 
      success: function(){ 
       self.render(); 
      } 
     }); 
    }, 

    render: function(){ 
     this.$el.html(
      this.template({countries: Countries.toJSON()}) 
     ); 
     return this; 
    } 
}); 

Để biết thêm thông kiểm tra here, và hy vọng điều này sẽ rất hữu ích :)

+0

Loại không bắt buộcLỗi: Không thể gọi phương thức 'html' của undefined @ this. $ El.html statement? bất kỳ ý tưởng? nhưng $ (this.el) .html (...) đã làm việc cho tôi – Urvish

+0

@Uvvish: Tôi đoán bạn đang sử dụng phiên bản cũ của backbone.js, phiên bản 0.9 mới có '' this. $ El' 'phím tắt cho' '$ (this.el)' '. Kiểm tra ở đây (http://documentcloud.github.com/backbone/#upgrading) – mouad

+0

'el' được coi là đối tượng DOM không phải là đối tượng jQuery. 'el: document.getElementById ('country_select_container')' hoặc 'el: $ (' # country_select_container '). Get (0) ' –

0

Bạn phải ràng buộc nhằm một phần tử DOM hiện có. Hãy thử điều này:

CountryView = Backbone.View.extend({ 

    el:'#country-selector', 

    events: { 
     "change #country-selector": "countrySelected" 
    }, 

... 

Hoặc trong trường hợp của bạn, bạn có thể vượt qua các tham số el như một cuộc tranh cãi với các nhà xây dựng:

new CountryView({el:'#country-selector'});

Nếu bạn không ràng buộc el đến một phần tử DOM hiện có, xương sống sẽ liên kết nó với một div theo mặc định. Vì vậy, các sự kiện của bạn sẽ bị sa thải tìm kiếm div > #country-selector và sẽ không khớp vì không có phần tử nào tồn tại.

+0

tôi đã cố gắng mới CountryView ({el: '# nước-selector'}); nhưng vẫn không thể cảnh giác? bất kỳ đề nghị nào khác? – Urvish

+0

Hãy thử với '$ ('# quốc gia-chọn')' ... – PhD

Các vấn đề liên quan