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

Global callbacks #170

Closed
elado opened this issue Sep 13, 2014 · 2 comments
Closed

Global callbacks #170

elado opened this issue Sep 13, 2014 · 2 comments
Assignees
Milestone

Comments

@elado
Copy link

elado commented Sep 13, 2014

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.

scope.select = function (category) {
  scope.selectedCategory = category
};

Category.on('before:destroy', function (attrs) {
  if (scope.selectedCategory && scope.selectedCategory.id == attrs.id) {
    scope.selectedCategory = null;
  }
});

Another use case would be to update counters on delete/create.

@elado
Copy link
Author

elado commented Sep 13, 2014

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.
Instead of $rootScope it'd be nice to use Post.$on.

Would a PR for that be welcome?

@jmdobry
Copy link
Member

jmdobry commented Sep 15, 2014

I've just about got it ready.

@jmdobry jmdobry self-assigned this Sep 15, 2014
@jmdobry jmdobry added this to the 1.0.0-rc.2 milestone Sep 15, 2014
jmdobry added a commit that referenced this issue Sep 15, 2014
jmdobry added a commit to js-data/js-data that referenced this issue Sep 15, 2014
@jmdobry jmdobry added done and removed in progress labels 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
Projects
None yet
Development

No branches or pull requests

2 participants