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

Sharing options model amongst multiple selectize #63

Closed
miketeix opened this issue Mar 31, 2015 · 1 comment · May be fixed by #64
Closed

Sharing options model amongst multiple selectize #63

miketeix opened this issue Mar 31, 2015 · 1 comment · May be fixed by #64

Comments

@miketeix
Copy link

The $watch on scope.options inside of onInitialize wasn't firing for me.

Line 106
        scope.$watch('options', function(){
          selectize.clearOptions();
          selectize.addOption(scope.options)
          selectize.setValue(scope.ngModel)
        }, true);

I believe it was because the reference $scope.options in my controller was being overwritten from within angular-selectize here:

Line 92 
scope.options = generateOptions( (scope.options || config.options || scope.ngModel).slice() );

I'm going to submit a pull-request with the changes that worked for me - would like to know what you think. Thanks again for this clean directive, Cheers!

miketeix pushed a commit to miketeix/angular-selectize that referenced this issue Mar 31, 2015
The reference to $scope.options was being overwritten. This saves what's returned from the generateOptions function and pushes it to an emptied scope.options while keeping its reference.

Resolves machineboy2045#63
@PhiLhoSoft
Copy link
Contributor

This fix breaks when combined with mine, allowing to provide options as an object. Since the object doesn't have (likely) a splice() method, I get a TypeError.
I have a fix, tested in https://plnkr.co/edit/8OuwS7?p=preview on both cases (array and object, one on persons, the other on e-mails).
Note: it includes another change, related to #69, but they are distinct.

Basically: I rename generateOptions to normailzeOptions, because it changes the data in place.
Instead of return $.map(...); I do:

    angular.forEach(data, function(opt, key) {
        if (typeof opt === 'string') {
            data[key] = createItem(opt);
        }
    });
    return data;

When I have generateOptions(scope.ngModel) I replace it with normalizeOptions(angular.copy(scope.ngModel), ie. I do the copy on the call site.
Thus, the main change becomes:

  if (scope.options) {
    // replace string options with generated options while retaining a reference to the same array
    normalizeOptions(scope.options);
  } else {
    // default options = [ngModel] if no options specified
    scope.options = normalizeOptions(angular.copy(scope.ngModel));
  }

I can do a pull request with these changes if you want.

PhiLhoSoft added a commit to PhiLhoSoft/angular-selectize that referenced this issue Aug 5, 2015
Was broken for options as object.
PhiLhoSoft added a commit to PhiLhoSoft/angular-selectize that referenced this issue Aug 5, 2015
- Fix machineboy2045#63
- Use registerOption instead of addOption, like Selectize does on
initialization
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 a pull request may close this issue.

2 participants