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

Fetched data does not get evaluated correctly (selectize empty) #59

Open
xeroxoid opened this issue Mar 18, 2015 · 12 comments
Open

Fetched data does not get evaluated correctly (selectize empty) #59

xeroxoid opened this issue Mar 18, 2015 · 12 comments

Comments

@xeroxoid
Copy link

Hi! Thanks for the module again!

So I have a Movie model that has _id, name, rating, etc.
When I have some pre-populated values i.e.

vm.selectedMovies = [{_id: '1', name: 'Bang'}, {_id: '2', name: 'Whatevs'}];

the selectize appears empty (i.e. the model binding does not seem to evaluate). I tried even preloading the collection from the server (to have all options available) but with no luck. My config options are as follows:

vm.movieSelectConfig = {
  valueField: '_id',
  labelField: 'name',
  searchField: 'name',
  sortField: 'name',
  // create: true,
  hideSelected: false,
  closeAfterSelect: true,
  preload: true,
  render: {
    option: function(movie) {
      return '<div class="selectOption">' + movie.name + '</div>';
    }
  },
  load: function(query, callback) {
    // if (!query.length) {
    //   return callback();
    // }
    return Movie.searchByName({
      'name': query
    }).$promise.then(function(results) {
      callback(results);
    });
  }
};

Any hints?

@xeroxoid
Copy link
Author

So what I did for now (but seems like a hack) is the following:

onInitialize: function(selectize){
  _.each(vm.currentUser.movies, function(movie) {
    selectize.addOption(movie);
    selectize.addItem(movie._id);
  });
}    

which seems to work. So the problem is that
a) the options are not populated before
b) my model is an array of full objects, thus I either need to _.pluck(vm.currentUser.movies, '_id'); or add each via iteration.

@adrienbourgeois
Copy link

A quick fix that I've found around that is to use the default valueField and labelField. So I've used objects like: {text:'the label', value:'the value'} and it's working fine. I didn't have the time to investigate further but so it looks like somewhere in the directive we are always using the defaults valueField and labelField even if some others were specified in the configuration.

@xeroxoid
Copy link
Author

xeroxoid commented Apr 2, 2015

Well that would mean that someone should change the models/object to suit the selectize dropdown which is imo unacceptable..

@davidsielert
Copy link

+1 needs way to bind to objects properly

@PhiLhoSoft
Copy link
Contributor

I had a similar issue. I found out that I was initializing $scope.config (whatever variable referenced in the config attribute of the directive) too late in my controller, so the binding didn't work: it is done only once, late updates are ignored.
I have just put the init of this scope variable in the body of the controller and it worked fine...

@azharkhan
Copy link

this seems to be a persistent issue with dynamic options.

i'm building out a list of columns from an uploaded CSV which is not available at the time of the config being loaded, since it has to be uploaded, and the scope variable is then updated with the list of columns, but I'm unable to see the selectize dropdown updated once the list of columns is available.

@PhiLhoSoft
Copy link
Contributor

For our projects, I made a SelectizeService which wraps calls to the Selectize component exposed from onInitialize.

I let Selectize do the loading of its options, so it just manages the loading class and updates correctly the options.
I do it this way:

    /**
     * Starts to load the data (by calling the loadFunction) after setting the loading class on the Selectize component.
     * @see SelectizeService#finishLoading
     */
    service.initLoading = function(selectizeControl, loadFunction)
    {
        // loadingFinished is a function given by Selectize, to call with the results once they are fetched
        var wrappedLoadFuntion = function(loadingFinished)
        {
            // Memorize the given function
            selectizeControl._finalizeLoading = loadingFinished;
            // Do the data loading call
            loadFunction();
        };
        // load() sets the loading class on the control,
        // then call the given function with an argument which is a function to call with the results.
        selectizeControl.load(wrappedLoadFuntion);
    };

    /**
     * To call on data loading completion, with the control (to remove the loading class),
     * and the received data (optional if options are created by other means).
     */
    service.finishLoading = function(selectizeControl, data)
    {
        selectizeControl._finalizeLoading(data);
    };

A bit convoluted, but it works.

@ElMassimo
Copy link

@machineboy2045 Any thoughts on this one?

@machineboy2045
Copy link
Owner

Try the latest version. I patched a couple bugs yesterday. If that doesn't help I'll look into it some more.

@machineboy2045
Copy link
Owner

@xeroxoid is this issue still present in v3?

@xeroxoid
Copy link
Author

Yep the issue is still present and it will always be until two-way object binding is in place. ATM you still have to pluck or set strings for values into setValue or addItem so you always need an onInitialize if you want to pre-select saved values.

@SearchingSoul
Copy link

Just found the following -
if my ng-model = "myModel"
following seems to work for me:
$scope.myModel = ["value1","value2","value3"] as long as they are these are part of the model.

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

No branches or pull requests

8 participants