Get / Set entity when dealing with relation attribute

627 Views Asked by At

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?

2

There are 2 best solutions below

8
On BEST ANSWER

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
  }
};
1
On

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