Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#254 preserve the attributes state (CSS, disabled, HTML5 data attrs) of <options> on model updates #255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't you be able to grab this.attributes directly without wrapping in jquery?

}).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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think $.fn.attr can take an object. No need for the _.each here...

option.attr(a.name, a.value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... unless it's worth skipping jQuery entirely here with a line like this.

});
}
};

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