Commits

Matthew Schinckel committed c2ccce4

(Hopefully) more logical change/reset handling.

Comments (0)

Files changed (3)

Add a comment to this file

django_countries/static/countries/flags/none.gif

Added
New image

django_countries/static/countries/js/dynamic-provinces.js

   
   function bindToCountry($province) {
     var $country = $($province.data('country-field'));
+    $country.data($country.val(), $province.val());
     if (!$country.data('dependent-fields')) {
       $country.data('dependent-fields', []);
     }
     $.each($province, function(i, el) {
       $country.data('dependent-fields').push('[name="' + el.name + '"]');
     });
-  
-    function buildProvincesField($element, data) {
-      var $newElement;
+    
+    var attributes = (function($element) {
       var id = $element.attr('id');
       var name = $element.attr('name');
       var classes = $element.attr('className');
   
       var attributes = ' name="' + name + '" id="' + id + '" class="' + classes +'" ';
+      
+      return attributes;
+    })($province);
+    
+    var elementData = $province.data();
+  
+    function buildProvincesField($element, data) {
+      var $newElement;
 
       if (!data) {
         $newElement = $('<input' + attributes + 'value="" type="text">');
           return '<option value="' + province.code + '">' + province.name + '</option>';
         }) + '</select>');    
       }
-  
-      $.each($element.data(), function(key, value) {
+      
+      // Should I actually add these as DOM data- attributes?
+      // To do that, I need to call dashify() on the keys. Later...
+      $.each(elementData, function(key, value) {
         $newElement.data(key, value);
       });
   
         },
         complete: function() {
           $province.replaceWith(newProvinceField);
-          $province = newProvinceField;
           $province.val(provinceForCountry());
           $province.change(function(e) {
             $country.data($country.val(), $province.val());

django_countries/widgets.py

     def render(self, name, value, attrs=None):
         attrs = attrs or {}
         attrs['data-country-field'] = '[name="%s"]' % self.country_field.html_name
-        if self.country_field.value().provinces_select:
-            return self.country_field.value().provinces_select.render(name, value, attrs)
+        country = self.country_field.value()
+        if country:
+            from django_countries.fields import Country
+            if not isinstance(country, Country):
+                country = Country(country)
+            if country.provinces_select:
+                return country.provinces_select.render(name, value, attrs)
+        else:
+            attrs['disabled'] = 'disabled'
+        attrs['type'] = 'text'
         return super(DynamicProvincesWidget, self).render(name, value, attrs)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.