Sha256: 318cb031df1a19b8b5b87eb6ccc24df83d4d923839b3ca351a70be07c4149667

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 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.select2("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().select2("destroy").hide();
    this.$state_input.hide();

    if (!this.states.fetched) {
      this.$state_select.show().select2().select2("disable");
    } 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().select2().select2("enable");
    } else {
      this.$state_input.prop('disabled', false).show();
    }
  }
})

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

5 entries across 5 versions & 1 rubygems

Version Path
solidus_backend-2.2.2 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-2.2.1 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-2.2.0 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-2.2.0.rc1 app/assets/javascripts/spree/backend/views/state_select.js
solidus_backend-2.2.0.beta1 app/assets/javascripts/spree/backend/views/state_select.js