iStefo / ember-select-2

DEPRECATED IN FAVOR OF https://github.com/cibernox/ember-power-select
MIT License
143 stars 119 forks source link

Example code #25

Open SteveMac1960 opened 10 years ago

SteveMac1960 commented 10 years ago

First of all I would like to say a big thank you for this plugin. We currently have a Apache Wicket based application that we just starting to migrate to EmberJS (using Ember-Cli). I am pretty new to Ember so although you have shown some working examples would it be possible to show the code behind them.

The main issue I am having is that I can create a select2 component on my page and I can click in it and it shows the options to select which are populated from my FIXTURES based model. What I cant seem to work out how to do is to save the selected option(s) and also show the selected option(s) when initially displaying the component. I assume it is something to do with the 'value' property of the component? but not sure how.

If you could provide the example code for the model, route and controller (if used) so I can see how to get this working that would be great.

iStefo commented 9 years ago

You are right, this is where the value binding comes into play. We divide into two cases:

1. You want to retrieve the selected objects in full

No further configuration is needed. The value binding is updated when the user selection changes.

{{select-2 value=mySelection content=myContent}}

So mySelection is an array of objects (a subset) of the myContent array. Use an observer to figure out changes on the array (so persist the selection e.g.).

2. You want to retrieve a specific property of the selected objects

Use select2 with optionValuePath set to a property of your model (e.g. id or name or something).

{{select-2 value=mySelection content=myContentIDs optionValuePath="id"}}

This way myContentIDs will only contain the IDs of the selected objects. Again, an observer can be used to be notified about changes.

I hope this solves your problem. If it doesn't, please let me know so I can improve the examples.

blimmer commented 9 years ago

This can probably be closed.

ashoksudani commented 7 years ago

@iStefo Hi iStefo. Actually I was running into the same issue but with more customisation. I need to search objects considering two properties in stead of one. With optionValuePath we can change one default property. But is there any way we can filter results based on our own function where we have the access of each object and passed search query term.

Right now I am trying to override "matcher" but in that function we are getting only the property of object which was defined by optionValuePath and search term. I need access of whole object.

Need your suggestion.

Thanks.