Skip to content

Commit

Permalink
nytimes#254 preserve the attributes state (CSS, disabled, HTML5 data …
Browse files Browse the repository at this point in the history
…attributes) of <options> on model updates
  • Loading branch information
sergey-dev committed Sep 29, 2014
1 parent 8d1ddd7 commit 26d9ad9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions backbone.stickit.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@
selectConfig = {};
var getList = function($el) {
return $el.map(function() {
return {value:this.value, label:this.text};
return {value:this.value, label:this.text, attr: Backbone.$(this).prop('attributes')};
}).get();
};
if ($el.find('optgroup').length) {
Expand Down Expand Up @@ -536,12 +536,17 @@
_.each(optList, function(obj) {
var option = Backbone.$('<option/>'), optionVal = obj;

var fillOption = function(text, val) {
var fillOption = function(text, val, attr) {
option.text(text);
optionVal = val;
// Save the option value as data so that we can reference it later.
option.data('stickit_bind_val', optionVal);
if (!_.isArray(optionVal) && !_.isObject(optionVal)) option.val(optionVal);
if (_.isObject(attr)) { // restore HTML5 data attributes, disabled attr, CSS, etc.
_.each(attr, function(a) {
option.attr(a.name, a.value);
});
}
};

var text, val;
Expand All @@ -552,7 +557,7 @@
text = evaluatePath(obj, selectConfig.labelPath),
val = evaluatePath(obj, selectConfig.valuePath);
}
fillOption(text, val);
fillOption(text, val, obj.attr);

// Determine if this option is selected.
var isSelected = function() {
Expand Down

0 comments on commit 26d9ad9

Please sign in to comment.