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

Observe attributes and update options on change. Update default text on every attribute update. #128

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
35 changes: 17 additions & 18 deletions chosen.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 20 additions & 14 deletions src/chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeo

restrict: 'A'
require: '?ngModel'
terminal: true
priority: 1
link: (scope, element, attr, ngModel) ->

element.addClass('localytics-chosen')
Expand All @@ -43,30 +43,36 @@ angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeo

# Options defined as attributes take precedence
angular.forEach attr, (value, key) ->
options[snakeCase(key)] = scope.$eval(value) if key in CHOSEN_OPTION_WHITELIST
if key in CHOSEN_OPTION_WHITELIST

# Observe attributes
# Update the value in options. Set the default texts again. Update message.
attr.$observe key, (value) ->
options[snakeCase(key)] = scope.$eval(value)
chosen.set_default_text();
updateMessage();

options[snakeCase(key)] = scope.$eval(value)

startLoading = -> element.addClass('loading').attr('disabled', true).trigger('chosen:updated')
stopLoading = -> element.removeClass('loading').attr('disabled', false).trigger('chosen:updated')

chosen = null
defaultText = null
empty = false

initOrUpdate = ->
if chosen
element.trigger('chosen:updated')
else
chosen = element.chosen(options).data('chosen')
defaultText = chosen.default_text

# Use Chosen's placeholder or no results found text depending on whether there are options available
removeEmptyMessage = ->
empty = false
element.attr('data-placeholder', defaultText)

disableWithMessage = ->
empty = true
element.attr('data-placeholder', chosen.results_none_found).attr('disabled', true).trigger('chosen:updated')
updateMessage = ->
if empty
element.attr('data-placeholder', chosen.results_none_found).attr('disabled', true)
else
element.removeAttr('data-placeholder')
element.trigger('chosen:updated')

# Watch the underlying ngModel for updates and trigger an update when they occur.
if ngModel
Expand Down Expand Up @@ -99,11 +105,11 @@ angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeo
if angular.isUndefined(newVal)
startLoading()
else
removeEmptyMessage() if empty
empty = isEmpty(newVal)
stopLoading()
disableWithMessage() if isEmpty(newVal)
updateMessage()
)

scope.$on '$destroy', (event) ->
$timeout.cancel timer if timer?
]
]