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

Event system #117

Closed
jmdobry opened this issue Aug 14, 2014 · 7 comments
Closed

Event system #117

jmdobry opened this issue Aug 14, 2014 · 7 comments

Comments

@jmdobry
Copy link
Member

jmdobry commented Aug 14, 2014

What about adding an event system to angular-data, such that whenever angular-data does anything it broadcasts the result of the action on $rootScope. This would provide an avenue for asynchronously reacting to changes within the datastore. Example:

DS.find('user', 5);

Somewhere else:

$rootScope.$on('DS.inject', function ($event, resourceName, injected) {
   /// do something in response
});

@kentcdodds @kenborge Thoughts?

@kentcdodds
Copy link
Contributor

I can see how this may be useful, but I don't have any current use case for it. What performance implications would this have? Could it be configurable? Like, choose which events to broadcast (specified, all, or none)?

@jmdobry
Copy link
Member Author

jmdobry commented Aug 14, 2014

I did some reading, and as of angular 1.2.16 there are no performance penalties for $rootScope.$broadcast. There is a 1:1 ratio of function invocations to number of listeners for the event.

@kentcdodds
Copy link
Contributor

If there's 0 perf implications then I'd have no problem throwing it in. Though, like I said, I currently have no use case for this (finished my side-project where I was using angular-data).

@kenjiqq
Copy link

kenjiqq commented Aug 14, 2014

My only concern would be that the angular faq seems to recommend not to use broadcast/on for module specific events. But I am not sure if that is outdated, or if this could be defined as atomic events i guess. https://github.com/angular/angular.js/wiki/Best-Practices

Is there still a 1:1 ratio if the on is registered on a child scope in a controller?

.controller(function($scope) {
  $scope.$on('DS.inject', function ($event, resourceName, injected) {
     /// do something in response
  });
})

@jmdobry
Copy link
Member Author

jmdobry commented Aug 14, 2014

Yes, 1:1 ratio.

Events that are relevant globally across the entire app

The only events I would implement to start with would be "DS.inject" and "DS.eject". Also, the eventing itself would be configurable. I'm thinking of 3 options.

/**
 * @doc property
 * @id DSProvider.properties:defaults.events
 * @name DSProvider.properties:defaults.events
 * @description
 * Whether to broadcast, emit, or disable DS events on the `$rootScope`.
 *
 * Possible values are: `"broadcast"`, `"emit"`, `"none"`.
 *
 * `"broadcast"` events will be [broadcasted](https://code.angularjs.org/1.2.22/docs/api/ng/type/$rootScope.Scope#$broadcast) on the `$rootScope`.
 *
 * `"emit"` events will be [emitted](https://code.angularjs.org/1.2.22/docs/api/ng/type/$rootScope.Scope#$emit) on the `$rootScope`.
 *
 * `"none"` events will be will neither be broadcasted nor emitted.
 *
 * Current events are `"DS.inject"` and `"DS.eject"`.
 *
 * Overridable per resource.
 */
Defaults.prototype.events = 'broadcast';

@kenjiqq
Copy link

kenjiqq commented Sep 7, 2014

There seems to be very little documentation about how the event system works, at least from what i found

@jmdobry
Copy link
Member Author

jmdobry commented Sep 7, 2014

There isn't much to document other than what is already documented here, here, and here. Whenever DS.inject() and DS.eject() are called they will fire DS.inject and DS.eject events, respectively. Whether the events are emitted (on $rootScope), broadcasted (on $rootScope) or disabled is controlled by DSProvider.defaults.events, which defaults to "broadcast".

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

3 participants