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

fix select "bind to multiple" implementation and sample #86

Closed
Thanood opened this issue Feb 3, 2016 · 12 comments
Closed

fix select "bind to multiple" implementation and sample #86

Thanood opened this issue Feb 3, 2016 · 12 comments
Assignees

Comments

@Thanood
Copy link
Collaborator

Thanood commented Feb 3, 2016

It's okay when selecting things but changing the view-model directly (which means: assign an object) breaks the sample.

@Thanood
Copy link
Collaborator Author

Thanood commented Feb 5, 2016

Please comment on this change:

this.valueObserver.value = $(this.element).val();
this.valueObserver.synchronizeValue();
this.valueObserver.synchronizeOptions();
this._suspendUpdate = true;
this.valueObserver.notify();
this._suspendUpdate = false;

I'm totally unsure if that's a good practice or more of an abuse of observerLocator.

@Thanood Thanood self-assigned this Feb 5, 2016
@Thanood
Copy link
Collaborator Author

Thanood commented Feb 5, 2016

After a chat session with @charlespockert I've reverted this to using a CustomEvent. Charles' version works in a plunk with a multiple select and without Materialize.

I've forked that plunk and changed it to use a multiple select here: http://plnkr.co/edit/2zwr4ExgNndjFVeiqUVW?p=preview

In Materialize bridge, though, it doesn't work. If you select some options in a multiple select, the bound view model variable is not updated.
I've noticed some differences, which may be related (or not, I'm not sure). The plunk triggers a MicroTaskQueue flush after the select value has been changed.

The Materialize version triggers that flush before the event is triggered. Still, the underlying array is mutated, has changed and this is detected correctly.

Here is what happens:
Aurelia's SelectValueObserver does not notify its subscribers. As far as I can see, it is not necessary since the ModifyCollectionObserver handles this.

In Charles' plunk, the above is exactly what happens. SelectValueObserver finishes and immediately triggers a MicroTaskQueue flush, resulting in ModifyCollectionObserver to be called which in turn calls its subscribers.

In Materialize bridge, SelectValueObserver finishes but then no ModifyCollectionObserver is called. Instead, a jQuery event handler is executed.

I don't know yet, what all this means but before I forget, I put it here.

@adriatic
Copy link
Member

adriatic commented Feb 5, 2016

I don't know yet, what all this means but before I forget, I put it here.

My attitude, exactly - with this additional explanation. I care a lot about understanding everything that happens in our team and it surrounding - so I subscribe to all these events as a service of Github. However, there is almost never a chance to spot one event and process it immediately and fully. So, saving the content of any discussion for your and other's later reading is really a must. Thank you Daniel !!

@Thanood
Copy link
Collaborator Author

Thanood commented Feb 5, 2016

Haha, I just noticed the color switcher selects are also broken now. 😄

@adriatic I will do this more often now since I know I forget things and the above is complicated enough to let me forget it over the weekend. 😄

@Thanood
Copy link
Collaborator Author

Thanood commented Feb 9, 2016

for reference: Dogfalo/materialize#2670 (comment)

@Thanood Thanood changed the title fix select "bind to object" implementation and sample fix select "bind to multiple" implementation and sample Feb 10, 2016
@Thanood
Copy link
Collaborator Author

Thanood commented Feb 10, 2016

I still don't get it really. The select <option>s seem to be updated but not the bound view model variable.
The plunk is different in that it updates a ChildInterpolationBinding while the bridge updates a SelectValueObserver.

The valueObserver which is commented out in current bridge code is updated. Still, if you issue a notify, nothing changes. Only when setting valueObserver.value directly, notify changes the bound array and the viewmodel is okay.

this.valueObserver.value = $(this.element).val();
this.valueObserver.notify();

In knockout/Durandal I'd do valueObserver.valueHasMutated() - so if anyone knows an equivalent.. 😄

Somehow I can't wrap my mind around that issue.

@Thanood
Copy link
Collaborator Author

Thanood commented Feb 20, 2016

@adriatic
Copy link
Member

Let me know how easy was it to read this code :-)

@Thanood
Copy link
Collaborator Author

Thanood commented Feb 21, 2016

Very easy. Everything is commented. 😄

Although I usually need to apply those things to some practice before I really understand it. In short, it's easy to understand what it does but I don't know yet why he does it.

One thing is apparent, though: He also circumvents problems with getting the selected value. Even by implementing dirty checking and using an own MutationObserver.
Maybe my workaround wasn't that "abusive" after all but there's another way to do exactly that without setting the observed value directly.

I will have to try it. 😄

@Thanood Thanood added the bug label Mar 4, 2016
@Thanood Thanood mentioned this issue Mar 13, 2016
@Thanood
Copy link
Collaborator Author

Thanood commented May 31, 2016

Could it be that easy...?
aurelia/framework#431

@Thanood
Copy link
Collaborator Author

Thanood commented Jun 1, 2016

No, it's not that easy...

Almost reproduced..
https://gist.run/?id=782e8648988619066b3065f6ba4655e1

Seems like I wouldn't have that problem if Materialize fired a custom event on change.

@Thanood
Copy link
Collaborator Author

Thanood commented Aug 2, 2016

Wow.. Turns out this is actually a problem (or intended behaviour) of combining string interpolation with valueConverters and array mutations. Data is set correctly but the output isn't updated..

Changing the output to a <ul> "fixed" this.

@Thanood Thanood closed this as completed Aug 2, 2016
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