Skip to content
This repository has been archived by the owner on Nov 30, 2018. It is now read-only.

value subscriber runs before option subscriber #28

Closed
six8 opened this issue Feb 3, 2012 · 6 comments
Closed

value subscriber runs before option subscriber #28

six8 opened this issue Feb 3, 2012 · 6 comments

Comments

@six8
Copy link

six8 commented Feb 3, 2012

If you have:

{ type: 'select', value: 'b', options: { a: 'A', b: 'B' }}

The value subscriber runs first and since there are no options yet, nothing gets selected.

If you reverse the keys:

{ type: 'select', options: { a: 'A', b: 'B' }, value: 'b'}

It works, however since you can't rely on the sort order of javascript object keys the code should make sure to run the value subscriber last (or at least after the options)

@daffl
Copy link
Owner

daffl commented Feb 3, 2012

Except for some special cases a JavaScript object will usually be traversed in the order it has been defined.
But you are right, it would totally make sense to be able to define dependencies.
I am just not sure what the best way to implement that would be...

@six8
Copy link
Author

six8 commented Feb 3, 2012

I had this for options:

years = {
  '': '',
  2012: 2012,
  2011: 2011,
  etc
}

When passed to options the sort couldn't be predicted. I had to use a list with [{ value: 2012, html: 2012}...] for options to ensure order was maintained. This is clearly one of those cases where order isn't maintained. This case generally doesn't appear in config options. However, it's an obscure problem that isn't obvious to casual observers.

I think the only way to do it would be to give each subscriber some sort of priority and apply them in that order or to maintain a list of the order to apply them. This would of course be a pain in the ass. You can also allow each subscriber to define an "after" subscriber for which you can determine dependency order, but that's a bit complicated too. I'll think about it and see if I can come up with a simpler option and send you a pull request.

@daffl
Copy link
Owner

daffl commented Feb 3, 2012

That is exactly the case where it doesn't work. V8 (Chrome) orders by the actual value, all the others when it has been defined. Unfortunately there is not much that can be done about that (except for being able to pass an array to the options value for years).
For the actual dependencies I was thinking about something like that:

$.dform.subscribe('value', function(options) {
    this.val(options);
}, ['options']);

This would assure that the options subscriber is run before if an option key exists.

@six8
Copy link
Author

six8 commented Feb 3, 2012

Looks good. I thought it would be fun to figure out a simple dependency graph in JS https://gist.github.com/1732686

@six8
Copy link
Author

six8 commented Feb 3, 2012

I found a much simpler way to do a topological sort on a graph so I updated the gist.

@daffl
Copy link
Owner

daffl commented Feb 3, 2012

This looks great, thanks! I will work on it on the weekend and hopefully get 0.1.4 finished up and released with the fix.

@daffl daffl closed this as completed Nov 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants