Skip to content

Commit

Permalink
Merge pull request #95 from gaui/master
Browse files Browse the repository at this point in the history
Fixed bug calling trigger before DOM is loaded (AngularJS 1.3)
  • Loading branch information
pkarl committed Jan 12, 2015
2 parents 6b27387 + 68b52f2 commit 807894a
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 103 deletions.
195 changes: 101 additions & 94 deletions chosen.js

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

23 changes: 14 additions & 9 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', ->
angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeout) ->

# 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 @@ -94,11 +94,16 @@ angular.module('localytics.directives').directive 'chosen', ->
valuesExpr = match[7]

scope.$watchCollection valuesExpr, (newVal, oldVal) ->
# There's no way to tell if the collection is a promise since $parse hides this from us, so just
# assume it is a promise if undefined, and show the loader
if angular.isUndefined(newVal)
startLoading()
else
removeEmptyMessage() if empty
stopLoading()
disableWithMessage() if isEmpty(newVal)
# Defer execution until DOM is loaded
timer = $timeout(->
if angular.isUndefined(newVal)
startLoading()
else
removeEmptyMessage() if empty
stopLoading()
disableWithMessage() if isEmpty(newVal)
)

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

0 comments on commit 807894a

Please sign in to comment.