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

feat(event): #24 add onGridStateChanged and onPaginationChanged events #28

Merged
merged 5 commits into from
Mar 13, 2018

Conversation

ghiscoding
Copy link
Owner

  • 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
@ghiscoding ghiscoding requested a review from jmzagorski March 10, 2018 22:54
@ghiscoding
Copy link
Owner Author

ghiscoding commented Mar 10, 2018

@jmzagorski
Would you mind doing a quick review of the Grid State events PR I just made.
I also modified the Grid State wiki and added a section at the end (with a note saying it will be available in next version). The new section is Grid State Events.

You can also try it by pulling the branch feature/grid-state-events

I'm planning on releasing a new version on Monday night.
Could you please also fix the bug #27 which is causing a few problems in the examples with presets.

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.

@jmzagorski
Copy link
Collaborator

👍 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`, {
Copy link
Collaborator

@jmzagorski jmzagorski Mar 11, 2018

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

Copy link
Owner Author

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?

Copy link
Collaborator

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>

Copy link
Owner Author

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.

Copy link
Collaborator

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

Copy link
Owner Author

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`, {
Copy link
Collaborator

@jmzagorski jmzagorski Mar 11, 2018

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)">
Copy link
Collaborator

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);
Copy link
Collaborator

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?

Copy link
Owner Author

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

Copy link
Collaborator

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));
Copy link
Collaborator

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

Copy link
Owner Author

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

Copy link
Collaborator

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.

Copy link
Owner Author

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

Copy link
Owner Author

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;
Copy link
Collaborator

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');
Copy link
Collaborator

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')

Copy link
Owner Author

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

Copy link
Collaborator

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');
Copy link
Collaborator

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') {
Copy link
Collaborator

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) {
Copy link
Collaborator

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?

Copy link
Owner Author

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

Copy link
Collaborator

@jmzagorski jmzagorski Mar 11, 2018

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

@ghiscoding
Copy link
Owner Author

@jmzagorski
Made a few changes according to your review. Thanks for that, it does help to get both side reviewed, especially in new features. I think I will continue asking you for review for future ones, if you don't mind thanks again.

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
Copy link
Collaborator

jmzagorski commented Mar 11, 2018

Nothing for Monday night most likely. I was in user meetings all last week so I did not get the chance to code a lot. I will work on #25 and hopefully #17 this week for next release

EDIT: I moved those over into the project "To Do" column and will update them when I start working on them this week.

@ghiscoding
Copy link
Owner Author

ghiscoding commented Mar 11, 2018

@jmzagorski
Thanks for the update.
I really like all the contributions that you have made to this project, I am also happy that you are now a collaborator. Thanks for all. 🎉

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

@jmzagorski
Copy link
Collaborator

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.

@ghiscoding ghiscoding merged commit b657e75 into master Mar 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants