Reputation: 1497
I know how to pull back an entity, can I can make changes to and say it's a "Company" with "People" in a child node. I can edit those people and save it and it all works great.
However, what I'm not 100% sure is how best to "create" new child object or entity with in that child collection and save it with the parent.
Adding Some Code as requested.
Html Code
<div data-ng-controller="updateTerritory as vm">
Company Name: {{vm.company.name}} : Territory: {{vm.company.territory.name || 'New Territory'}}<br />
<input type="text" data-ng-model="vm.company.name"/>
<input type="text" data-ng-model="vm.company.territory.name" />
<button data-ng-click="vm.save()">Save Territory</button>
</div>
Route Url: #/company/territory/71/new
Angular Controller
angular.module('app').controller(controllerId,
['$location', '$routeParams', 'common', 'datacontext', 'helper', updateTerritory]);
function updateTerritory($location, $routeParams, common, dc, helper) {
var vm = this;
vm.company = undefined;
activate();
function activate() {
common.activateController([getRequestedCompany()], controllerId);
}
function getRequestedCompany() {
var id = $routeParams.id;
var tId = $routeParams.tId;
//if (tId === 'new') {
//vm.company.territory = dc.company.territory.create();
//return vm.company;
//}
return dc.company.getById(id)
.then(function (data) {
vm.company = data;
}, function (error) {
logError("Unable to get company " + id);
});
}
function save() {
return dc.save()
.then(function (saveResult) {
}, function (error) {
vm.isSaving = false;
});
}
}
DataContext Service (dc). this works fine for everything so far, even child collections
function save() {
return manager.saveChanges()
.then(saveSucceeded, saveFailed);
function saveSucceeded(result) {
logSuccess("Saved Data", result, true);
}
function saveFailed(error) {
throw error;
}
}
It save's it to the Entity but not to the db, though if I update any properties like Company.Name that saves to the DB. If I try and update say Company.Territory.Name, which doesn't exist and has it's down table in the DB, it doesn't update.
So recap, I can edit and save root properties and child collections, but I'm not sure how to create new entity on the child collection.
Upvotes: 1
Views: 212
Reputation: 3108
var newCust = manager.createEntity('Customer', {name:'Acme'});
Just do that to create the 'child' object, and then add the new object to the relevant collection on the parent object.
Upvotes: 2