Sha256: 9ecde7f70764fd4d8b0dcd80848d01b894d327fff8292906162af6cbca55887a

Contents?: true

Size: 1.93 KB

Versions: 137

Compression:

Stored size: 1.93 KB

Contents

Spree.Views.StateSelect = Backbone.View.extend({
  initialize: function() {
    this.states = {} // null object

    this.$state_select = this.$('.js-state_id');
    this.$state_input = this.$('.js-state_name');

    // read initial values from page
    this.model.set({
      state_name: this.$state_input.val(),
      state_id: this.$state_select.val()
    })

    this.updateStates();
    this.listenTo(this.model, 'change:country_id', this.updateStates)
    this.render();
  },

  events: {
    "change .js-state_name": "onChange",
    "change .js-state_id": "onChange",
  },

  onChange: function() {
    this.model.set({
      state_name: this.$state_input.val(),
      state_id: this.$state_select.val()
    })
  },

  updateStates: function() {
    this.stopListening(this.states);
    var country_id = this.model.get("country_id");
    if (country_id) {
      this.states = Spree.Views.StateSelect.stateCache(country_id);
      this.listenTo(this.states, "sync", this.render);
    }
    this.render();
  },

  render: function() {
    this.$state_select.empty().hide().prop('disabled', true);
    this.$state_input.hide().prop('disabled', true);

    if (!this.model.get('country_id') || !this.states.fetched) {
      this.$state_select.show();
    } else if (this.states.length) {
      var $state_select = this.$state_select;
      this.states.each(function(state) {
        $state_select.append(
          $('<option>').prop('value', state.id).text(state.get("name"))
        );
      })
      this.$state_select.val(this.model.get("state_id"))
      this.$state_select.show().prop("disabled", false);
    } else {
      this.$state_input.show().prop('disabled', false);
    }
  }
})

Spree.Views.StateSelect.stateCache = _.memoize(function(country_id) {
  var states = new Spree.Collections.States([], {country_id: country_id})
  states.fetched = false;
  states.fetch({
    success: function() {
      states.fetched = true;
    }
  });
  return states;
});

Version data entries

137 entries across 137 versions & 2 rubygems

Version Path
solidus_backend-4.4.1 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.4.0 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.3.4 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.2.4 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.1.5 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.3.3 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.3.2 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.1.4 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.3.1 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.3.0 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.2.3 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.1.3 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.0.4 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-3.4.6 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.0.3 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.1.2 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.2.2 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-3.4.5 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.2.1 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-4.2.0 app/assets/javascripts/spree/backend/views/state_select.js