-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
feat(event): #24 add onGridStateChanged and onPaginationChanged events #28
Conversation
ghiscoding
commented
Mar 10, 2018
- add onGridStateChanged
- add onPaginationChanged event that Angular-Slickgrid can then trigger Grid State onGridStateChanged event
- add better onFilterChanged & onSortChanged events
- add onGridStateChanged - add onPaginationChanged event that Angular-Slickgrid can then trigger Grid State onGridStateChanged event - add better onFilterChanged & onSortChanged events
@jmzagorski You can also try it by pulling the branch I'm planning on releasing a new version on Monday night. I also need this to be merged before starting the work on Compound Filter since it changes code in Filter Service which this PR also does. |
👍 i plan on doing some work tomorrow so I can look at the bug and this PR |
@@ -171,6 +171,17 @@ export class SlickPaginationCustomElement { | |||
} else { | |||
throw new Error('Pagination with a backend service requires "BackendServiceApi" to be defined in your grid options'); | |||
} | |||
|
|||
// dispatch the changes to the parent component | |||
this.elm.dispatchEvent(new CustomEvent(`on-pagination-changed`, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be good if we always use the eventPrefix on dispatched events so we know the events will be unique to the library. That prefix is in the AureliaSlickgridCustomElement, but it might be good if we put it in a separate constants file (of if another applicable file exists) so other custom elements can use it too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to use the sg
prefix because it represents the SlickGrid events not Aurelia-Slickgrid events. I didn't feel the need to have a prefix for internal one. Do you really think it's necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefixed the events with sg
to uniquely identify any event coming from your library that is exposed to the user (i see this in many aurelia libraries). Since these events are bubbling from your library they will always be exposed to the user. In my application I can bind to that on-pagination-changed
event and any other event, and if we prefixed some events and not others that are exposed I thought that may come across as inconsistent. For example:
<template>
<aurelia-slickgrid
on-pagination-changed="handlePagination()"
sg-on-cell-click="handleClick()">
</aurelia-slickgrid>
</template>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I would still rather make a distinction between my own events versus the SlickGrid events which always have an event
and args
and are not working the same as internal ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I understand. You may still want a different prefix because another library can have the same event name on-pagination-changed
that would be in conflict with yours since they all bubble up the DOM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok perhaps as
(Aurelia-Slickgrid), hmm no that's too confusing lol...
or asg
that's probably better
@@ -286,6 +294,14 @@ export class AureliaSlickgridCustomElement { | |||
} | |||
} | |||
|
|||
// expose GridState Service changes event through dispatch | |||
this.gridStateSubscriber = this.ea.subscribe('gridStateService:changed', (gridStateChange: GridStateChange) => { | |||
this.elm.dispatchEvent(new CustomEvent(`on-grid-state-service-changed`, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about the eventPrefix
@@ -3,6 +3,6 @@ <h2>${title}</h2> | |||
<div class="subtitle" innerhtml.bind="subTitle"></div> | |||
|
|||
<aurelia-slickgrid grid-id="grid1" dataview.bind="dataview" grid.bind="gridObj" column-definitions.bind="columnDefinitions" | |||
grid-options.bind="gridOptions" dataset.bind="dataset"> | |||
grid-options.bind="gridOptions" dataset.bind="dataset" on-grid-state-service-changed.delegate="gridStateChanged($event.detail)"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about the eventPrefix
@@ -152,6 +152,10 @@ export class Example4 { | |||
} | |||
} | |||
|
|||
gridStateChanged(gridState) { | |||
console.log(gridState); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you going to put some logic here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's just for demo purposes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooh didn't realize it was on example4.ts
// we also need to add our filter service (with .bind) to the callback function (which is outside of this service) | ||
// the callback doesn't have access to this service, so we need to bind it | ||
const self = this; | ||
this._slickSubscriber.subscribe(this.attachBackendOnFilterSubscribe.bind(this, self)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since you are binding this
to the scope of attachBackendOnFilterSubscirbe
you do not need const self = this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The this
that is in there seems to be in fact the SlickGrid event
and args
, which is why I added the self
. I haven't tried (this
, this
) though but that would look funny
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You only need this._slickSubscriber.subscribe(this.attachBackendOnFilterSubscribe.bind(this));
. Then your attachBackendOnFilterSubscribe
will have the context of your service already, so using this
within the method will work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok will give that a try a bit later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you were right about this, I didn't know that simply doing the .bind(this)
was enough. Good to know and it cleans up the code a lot. Thanks :)
// subscribe to the SlickGrid event and call the backend execution | ||
// we also need to add our filter service (with .bind) to the callback function (which is outside of this service) | ||
// the callback doesn't have access to this service, so we need to bind it | ||
const self = this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about being able to remove self
@@ -38,6 +47,7 @@ export class SortService { | |||
backendApi.preProcess(); | |||
} | |||
const query = backendApi.service.onSortChanged(event, args); | |||
self.emitSortChanged('remote'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when self
is removed this can be this.emitSortChanged('remote')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually no, the function is a callback (of the Filter DOM element) and has no knowledge of this service anymore, which is why I needed to add another self
argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, it is a callback, but you are already binding this
to the callback. So if you do this._slickSubscriber.subscribe(this.attachBackendOnSortSubscribe.bind(this));
you can do this.emitSortChanged('remote')
I tested it out and it worked.
@@ -77,6 +79,9 @@ export class FilterService { | |||
// call the service to get a query back | |||
const query = await backendApi.service.onFilterChanged(event, args); | |||
|
|||
// emit an onFilterChanged event | |||
self.emitFilterChanged('remote'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when self
is removed this can be this.emitFilterChanged('remote')
* Other services, like Pagination, can then subscribe to it. | ||
* @param sender | ||
*/ | ||
emitSortChangedBy(sender: string) { | ||
this._subscriber.subscribe(() => this.onSortChanged.publish('sortService:changed', `onSortChanged by ${sender}`)); | ||
emitSortChanged(sender: 'local' | 'remote') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment as in the filter service where the logic may be able to be simplified if only two strings are allowed
@@ -85,11 +85,11 @@ export class SlickPaginationCustomElement { | |||
} | |||
|
|||
dispose() { | |||
if (this._filterSubcription) { | |||
this._filterSubcription.dispose(); | |||
if (this._filterSubscriber) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you need the null/undefined if
logic here since you are setting these in the constructor and this will always pass as true
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually didn't test the Build Library, but in some cases I think it might be undefined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see if it will build or that is an odd typescript error
@jmzagorski You can have a final review if you wish, I will merge it on Monday night and release a new version at that time. Just curious, did you have anything in the pipeline? If so, let me know and I could wait a bit for a new release. |
@jmzagorski I forgot to ask, have you tried doing a branch directly on the project? Are we good without the Dev branch? If so I will delete the TODO notes in the Project. Thx |
You're welcome, and I appreciate all of your effort to make this such a great library for Aurelia. You can delete dev. I have been branching off of your master with my feature/fix branches. |