Skip to content

Commit

Permalink
Remove unnecessary timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Rosen committed Nov 29, 2013
1 parent faf0608 commit d41d373
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 93 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-chosen-localytics",
"version": "1.0.2",
"version": "1.0.3",
"main": "chosen.js",
"ignore": [
"src",
Expand Down
174 changes: 90 additions & 84 deletions chosen.js

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

23 changes: 15 additions & 8 deletions src/chosen.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('localytics.directives', [])

angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeout) ->
angular.module('localytics.directives').directive 'chosen', ->

# This is stolen from Angular...
NG_OPTIONS_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+(.*?)(?:\s+track\s+by\s+(.*?))?$/
Expand Down Expand Up @@ -38,7 +38,7 @@ angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeo
link: (scope, element, attr, ctrl) ->

element.addClass('localytics-chosen')

# Take a hash of options from the chosen directive
options = scope.$eval(attr.chosen) or {}

Expand All @@ -49,8 +49,16 @@ angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeo
startLoading = -> element.addClass('loading').attr('disabled', true).trigger('chosen:updated')
stopLoading = -> element.removeClass('loading').attr('disabled', false).trigger('chosen:updated')

initialized = false
empty = false

initOrUpdate = ->
if initialized
element.trigger('chosen:updated')
else
element.chosen options
initialized = true

removeEmptyMessage = ->
empty = false
element.find('option.empty').remove()
Expand All @@ -59,21 +67,21 @@ angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeo
empty = true
element.empty().append("""<option selected class="empty">#{message}</option>""").attr('disabled', true).trigger('chosen:updated')

# Init chosen on the next loop so ng-options can populate the select
$timeout -> element.chosen options

#Watch the underlying ng-model for updates and trigger an update when they occur.
# Watch the underlying ngModel for updates and trigger an update when they occur.
if ctrl
origRender = ctrl.$render
ctrl.$render = ->
origRender()
element.trigger('chosen:updated')
initOrUpdate()

# This is basically taken from angular ngOptions source. ngModel watches reference, not value,
# so when values are added or removed from array ngModels, $render won't be fired.
if attr.multiple
viewWatch = -> ctrl.$viewValue
scope.$watch viewWatch, ctrl.$render, true
# If we're not using ngModel (and therefore also not using ngOptions, which requires ngModel),
# just initialize chosen immediately since there's no need to wait for ngOptions to render first
else initOrUpdate()

# Watch the disabled attribute (could be set by ngDisabled)
attr.$observe 'disabled', (value) -> element.trigger 'chosen:updated'
Expand All @@ -91,4 +99,3 @@ angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeo
removeEmptyMessage() if empty
stopLoading()
disableWithMessage(options.no_results_text || 'No values available') if isEmpty(newVal)
]

0 comments on commit d41d373

Please sign in to comment.