ui-select2 icon indicating copy to clipboard operation
ui-select2 copied to clipboard

Added watch to uiSelect2 attribute for options changes

Open natebundy opened this issue 12 years ago • 7 comments

This is my attempt at doing something about #36. I would love feedback on this, especially if there is a better way to check if attrs.uiSelect2 is an object literal or something that should be resolved on the scope.

I'll write tests for the new functionality after I get some feedback.

natebundy avatar Jul 09 '13 18:07 natebundy

This is wrong.

Instead, wrap this: https://github.com/angular-ui/ui-select2/blob/380dc0de0ab18fcfa5970f5cfff5bc50d7287576/src/select2.js#L145-L156

in this:

$scope.$watch(attrs.uiSelect, function(newOptions, oldOptions){
  ... (highlighted code goes here, using newOptions instead of evaluating) ...
}, true);

ProLoser avatar Jul 09 '13 21:07 ProLoser

Sounds good, but we'll still need a way to check if attrs.uiSelect is an object literal, correct? Trying to watch it in that case will throw an error. try/catching angular.fromJson to check for that seems like a hack to me, but I don't know a better way to go about it.

natebundy avatar Jul 10 '13 14:07 natebundy

No, just do:

if (newOptions) {
  // assume truthy values are always objects
  ...
}

If it's ever a truthy non-object, then someone is implementing it incorrectly.

ProLoser avatar Jul 10 '13 18:07 ProLoser

The latest revision fails several tests but seems to work in practice. I'll add some tests for the new functionality soon and check if the existing tests are failing for good reason or because of the change in behavior. Thanks again for pointing me in the right direction.

natebundy avatar Jul 11 '13 23:07 natebundy

It turns out those tests were failing for good reason and my instincts were right to call select2 with the initial options. All tests passing now, just need to add tests for new behavior.

natebundy avatar Jul 12 '13 15:07 natebundy

This is perfect. Solved all my issues with options not loading (it would run scope.$eval before my directive was even loaded).

Thanks! This PR should be accepted.

michaelowens avatar May 02 '14 10:05 michaelowens

Thank you, this PR just saved me a lot of time! Though I would feel even more comfortable if it was accepted into the next release ... ;)

GammaSQ avatar Jul 31 '14 09:07 GammaSQ