Created
August 30, 2014 12:45
-
-
Save Ulv/ac611b3ea94985cbb290 to your computer and use it in GitHub Desktop.
linked selects in coffeescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# связанные селекты | |
# secondarySelect всегда disabled кроме след. случаев: | |
# - в primarySelect есть значение | |
# - значение primarySelect != 0 | |
### | |
$.fn.extend | |
bestdiplomSelect: (options) -> | |
@defaultOptions = | |
### пустое значение ### | |
emptyVal: "0" | |
### основной селект, объект jQuery ### | |
primarySelect: null | |
### зависимый селект, объект jQuery ### | |
secondarySelect: null | |
settings = $.extend({}, @defaultOptions, options) | |
### .attr("disabled", true) ### | |
$.fn.disableSelect = -> | |
@each -> | |
@disabled = true | |
### .attr("disabled", false) ### | |
$.fn.enableSelect = -> | |
@each -> | |
@disabled = false | |
### пустое значение? ### | |
isEmpty = (val) -> | |
val == settings.emptyVal | |
### основной функционал ### | |
handleLists = () -> | |
$(settings.secondarySelect).disableSelect().trigger "refresh" | |
$(settings.primarySelect).on "change", () -> | |
if isEmpty($(settings.primarySelect).val()) | |
$(settings.secondarySelect) | |
.disableSelect() | |
.trigger "refresh" | |
.val settings.emptyVal | |
else | |
$(settings.secondarySelect) | |
.enableSelect() | |
.trigger "refresh" | |
return @each () -> | |
handleLists() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment