2

I'm working in a Wakanda environnement with the angular-wakanda connector.

Assume this model : Employee(firstName,lastName,company) and Company(name)

In the employee form, I have a select input that is filled with companies names (which is a entity collection).

I have set a ng-model="selectedCompany" in the select

When I select one company and perform a save, the value I get represent the value I have pass in the value of my option (ID or name).

When I assign the value to a new entity using the $create() method, I don't know which way to set the entity to the relation attribute. I assume that I have to give an entity. The thing is that I already have the entitycollection with all the entities. So I don't see the reason why should I query again the server just to assign somthing that I have already.

So the thing is that we should have a method like $find or $getById that will not do a request on the server but get the entity that is already loaded in the entityCollection. For now I use my own method that do a simple loop over my array. (I can share the code if anybody need it)

Am I missing a way to do that?

Ganbin
  • 2,004
  • 2
  • 12
  • 19

2 Answers2

2

Assuming your selectedCompany variable contains an entity (of type Company), you only have to pass this variable on the $create() call with the property name like this:

var newEmployee = ds.Employee.$create({
  firstName: 'foo',
  lastName: 'bar',
  company: $scope.selectedCompany
});

If selectedCompany contains only company name or ID, it's on you to have an array to map the companies with their name or ID. Something like that:

//When retrieving collection
var companyMap = [];
companies.forEach(function (company) {
  companyMap[company.ID] = company;
}

//When you want your company with its ID
var company = companyMap[$scope.selectedCompany];

But the simplest way is the first, with your <select> directly iterating through entity collection, so that you can deal with entities.


Edit: Retrieving object with ng-option

To get the object on which you iterate on your select, you can use ng-option like this:

<select
  ng-model="selectedCompany"
  ng-change="companyChanged()"
  ng-options="c.name for c in companies"
>
</select>

On the controller

$scope.selectedCompany = null;
$scope.companyChanged = function () {
  if ($scope.selectedCompany) {
    //selectedCompany is your company entity
    //you can manipulate it like any other entity
    $scope.selectedCompany.$save(); //for example
  }
};
Blackus
  • 6,883
  • 5
  • 40
  • 51
  • Did you try your first statement? Because I have try and I cannot pass a entity value to my html option element. If I do so, I just set a string that represent a JSON object that I cannot pass to the entity relation attribute. So for me the maping solution is the best because I can get the object then attribute is waiting for. – Ganbin Feb 26 '16 at 09:44
  • Here is my html code `` and you want me to do that : `` ? But that will print the JSON string like I say and not really get the entity object. – Ganbin Feb 26 '16 at 09:47
  • 1
    You can do it by using `ng-option` directive on your ` – Blackus Feb 26 '16 at 09:56
  • 1
    Great. Thanks. I didn't knew about this ng-options. That what I was looking for. – Ganbin Feb 26 '16 at 10:24
  • That make me fall in another problem. It's when I want to select the option when editing an item. Before it was working because I use the name as value, But when we use ng-options, I cannot set the current selected value. I try to give the ID, the entity itself, but I cannot achieve to select an option. – Ganbin Feb 26 '16 at 10:40
  • I fall in big inconsistency for the way I can get the selected value and the way I can set it. If I get the value as an object, I cannot set it. If I get the object as an ID (add `track by category.ID`) then I can set the right value when editing, but then I cannot get straight the object. So I will always have to make a query/find/map of my array either to get or the set the value. I hope I do something wrong, but I see that is a really basic stuff to do so why there is so much inconsistency? – Ganbin Feb 26 '16 at 11:07
  • 1
    I'm afraid I don't have an answer to this question, which is more Angular related than to Wakanda. Select handling has always been a pain for me to. – Blackus Feb 26 '16 at 11:43
  • Yes this is more a angular/javascript problem to solve. But thanks for the time and effort. It help me to see the real problem. To workaround is not a problem to find. But it is always nice to know what our code do and why we have to do this way. – Ganbin Feb 26 '16 at 13:45
1

I think that your real problem is that you iterate on name/ID of each entity of company Collection.

This is the bad way to iterate on collections :

<select ng-model="selectedCompany" ng-options="company.nom as company.nom for i in collenctionOfCompanies">
    <option value=""></option>
</select>

In this case selected company will contain only the name of the company.

But if you change your code as this :

<select ng-model="selectedCompany" ng-options="company as company.nom for i in collenctionOfCompanies">
    <option value=""></option>
</select>

Then selectedCompany will contain the entity

walid chafai
  • 106
  • 4
  • The problem was that I was not using ng-options, but ng-repeat on the ` – Ganbin Feb 26 '16 at 10:35