-
Notifications
You must be signed in to change notification settings - Fork 77
Global callbacks #170
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
Comments
Saw your answer on #169 I wrote a bunch of code to help me achieve that behavior globally. (Pardon my coffee) app.run ($rootScope, DS) ->
phases = ['before', 'after']
events = ['validate', 'create', 'update', 'destroy', 'inject']
DS.defaults.events = 'emit'
# patch: eject before destroy
globalCustomHooks =
'before:destroy': (resourceName, attrs) ->
DS.eject(resourceName, attrs[DS.definitions[resourceName].idAttribute])
true
# TODO 'before:create' - inject as pending, 'after:create' - remove pending flag
fire = (resourceName, phase, event, attrs, cb) ->
console.log "#{resourceName}:#{phase}:#{event}"
$rootScope.$emit "#{resourceName}:#{phase}:#{event}", attrs
customFn = globalCustomHooks["#{phase}:#{event}"]
customFn?(resourceName, attrs)
cb null, attrs if cb
phases.forEach (phase) ->
events.forEach (event) ->
DS.defaults[phase + _.str.capitalize(event)] = (resourceName, attrs, cb) ->
fire resourceName, phase, event, attrs, cb
$rootScope.$on 'DS.eject', (e, resourceName, attrs) ->
fire resourceName, 'after', 'eject', attrs Usage is something like: $rootScope.$on 'categories:after:eject', (e, category) ->
if scope.selectedCategory?.id == category.id
scope.selectedCategory = null
$rootScope.$on 'articles:after:inject', (e, article) ->
if article.categoryId
articleCountInCategory[article.categoryId]++
$rootScope.$on 'articles:after:update', (e, article) ->
alert "i was updated!" It also fixes #169 destroy case, I'll implement the create and see how it goes. Would a PR for that be welcome? |
I've just about got it ready. |
Merged
jmdobry
added a commit
to js-data/js-data
that referenced
this issue
Sep 15, 2014
This was referenced Sep 15, 2014
jmdobry
added a commit
to js-data/js-data
that referenced
this issue
Sep 15, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would be really helpful to track model hooks globally.
Let's say I have a list of categories to choose from, and on a different place on the screen user can delete categories. When they delete the selected category, I want to reset the scope variable.
Another use case would be to update counters on delete/create.
The text was updated successfully, but these errors were encountered: