Custom DataService adapter saveChanges method to set entities to Unchanged - breeze

I've implemented a custom DataService adapter for BreezeJS - I wanted to use Breeze with a RESTful back end service (not OData or ASP.NET Web API).
So far - decent results after a learning curve.
I'm having an issue that when I call save changes - afterwards my entities on the client do not get marked as 'Unchanged'. They keep the same entityState.
I assume it has something to do with the success handler of the AJAX request to the backend service (looking at the source code to the WebAPI adapter):
success: function(data, textStatus, XHR) {
if (data.Error) {
// anticipatable errors on server - concurrency...
var err = createError(XHR);
err.message = data.Error;
deferred.reject(err);
} else {
// HACK: need to change the 'case' of properties in the saveResult
// but KeyMapping properties internally are still ucase. ugh...
var keyMappings = data.KeyMappings.map(function(km) {
var entityTypeName = MetadataStore.normalizeTypeName(km.EntityTypeName);
return { entityTypeName: entityTypeName, tempValue: km.TempValue, realValue: km.RealValue };
});
var saveResult = { entities: data.Entities, keyMappings: keyMappings, XHR: data.XHR };
deferred.resolve(saveResult);
}
},
It looks like the response includes an array of 'Entities'. What do these 'Entities' look like? It echoes what the client sent with an updated entityAspect.entityState value (server responses with 'Unchanged')?
Is that what should be passed into the deferred.resolve call?

I've got a working solution for this.
In a nutshell here's what is required for the object that is passed to the
deferred.resolve(saveResult);
Call in the success handler of the save change AJAX request.
Server response should include information about how to map from the client generated id to the server generated id (if the server generated one). This can be one keyMapping property returned in the response (like the Breeze API controller does) or what my service does is return a keyMapping property as a child property of a particular resource
The client code should create an array of objects that look like:
{ entityTypeName: "fully qualified entity type name",
tempValue: "client generated id",
realValue: "server generated id"
}
this array is the keyMappings property of the saveResult object
the entities property of the saveResult object is a flat list of all the entities that were modified from the server. Because of the design of my service API, it can return an entity, and child entities embedded in it, which I had to traverse and pull out into a flat list. Additionally these entity objects should be 'raw' and not include the entityAspect property or anything Breeze might interpret as a 'real' entity.
Also - something that can also be helpful is to look at the new sample from the Breeze folks - the MongoDB Breeze sample. They've implemented a custom dataServiceAdapter that hooks up their NodeJS/MongoDB backend. That provided some additional insight as well.
Good luck!

Related

sending multiple objects to back-end post method using angular 4 http services

Hi I am trying to send two objects using http post method to backend
my environment is angular4, typescirpt, asp.net MVC 5
but it is throwing 500 internal server error
the same approach if I am passing single object to the backend my backend method is getting called
here is the code with passing single object
clientSidePostCall(Results:any,Details:any):Observable<any>{
return this._http.post(Global.ENDPOINT +'BackendMethod/',Results)
.map((response: Response) => <any>response.json())
.catch((err:any) => { throw err; });
}
the above code is working fine if I send Results object to BackendMethod if it is expecting single parameter
The same code is not working if I send multiple objects to backendMethod when it is expecting two objcets.
clientSidePostCall(Results:any,Details:any):Observable<any>{
return this._http.post(Global.ENDPOINT +'BackendMethod/',Results,Details)
.map((response: Response) => <any>response.json())
.catch((err:any) => { throw err; });
}
The above code is not working and throwing 500 internal server error
here is my backend method signature
[HttpPost]
public HttpResponseMessage BackendMethod([FromBody] resultsType Results, [FromBody] detailsType Details)
please help me with this
and I am having another doubt that can we pass object in http.get in angular 4 and typescript
In your angular code make the Results, and Details be properties of a larger object... So send this object:
const myPostBody = { resultsType: Results, detailsType: Details }
return this._http.post(Global.ENDPOINT +'BackendMethod', myPostBody)
.map((response: Response) => <any>response.json())
.catch((err:any) => { throw err; });
}
Also make sure that your API class type Results actually matches the Class Results that you're sending it
I don't know how asp.net works but the third argument of HttpClient.post is not for another post body. In fact a post request cannot have two bodies. If your backend expects an array, do:
this._http.post(url, [Results, Details])
Wrapping up child entities in a parent entity is required for HTTP Post. The child entity would usually be a business entity.
On the receiving service end, you will use the entity names as properties to receive the individual items. All this assume your entity itself is serializable.

SAP UI5 Modifying an Odata model obtained from manifest before setModel()

I am creating my first SAP UI5 app and I have a need where i want to set an additional property to the data returned from an Odata service before i set it as a model to in the table view
However i am unable to access the data as the getProperty returns an empty object. Below is the code on the onInit() method of my controller . I am obtaining the model from my manifest.json as it looks a clean way to do things.
var rList = this.getOwnerComponent().getModel("Entities");
var localPromise = this.getPromise(rList, "/");
localPromise.done(function() {
console.log(rList.getProperty("/"));
// here i would like to do the manipulations and then set the view
}.bind(this));
},
i assume the getPromise method must take care of the asynchronous execution part:
getPromise: function(oModel, pathToTestForData) {
var deferred = $.Deferred();
if (oModel.getProperty(pathToTestForData))
deferred.resolve(); //Data already loaded
else
oModel.attachRequestCompleted(deferred.resolve); //Waiting for the event
return deferred.promise();
}
What i want to do is the following:
1) make the Odata two way bind.
2) set another property : anotherProperty : false on each returned Odata record
However as rList.getProperty("/") inside the done of the promise is empty, i am at a roadblock, Also how to access the data (if it was there)

breeze: querying local cache when using client-side model

Consider the below code. It works fine when getting data from the server. I have a custom data adapter (staffManagemetnService) which creates client-side entities from the json returned by the server.
However, if I make a call to executeQueryLocally, it fails and raises the following exception: Cannot find an entityType for resourceName: 'GetInternalResourcesByCompetence'. Consider adding an 'EntityQuery.toType' call to your query or calling the MetadataStore.setEntityTypeForResourceName method to register an entityType for this resourceName
var query = breeze.EntityQuery.from('GetInternalResourcesByCompetence').withParameters(parameters);
var result = self.manager.executeQueryLocally(query.using(dataService.staffManagementService));
if (result) {
return $q.resolve(result);
} else {
return this.manager.executeQuery(query.using(dataService.staffManagementService))
.then(function (data) {
return data.results;
})
.catch(function (err) {
logError('Restrieving resources days off failed', err, true);
});
}
I'm not sure what this means. Should it not work out-of-the-box since I've specifically asked breeze to use the custom dataAdapter ?
It's important to different between resource names and entity type names. Resource names are usually part of an endpoint and in plural (eg orders). Type names are typically singular (eg order).
Locally breeze cannot do much with the resource name, since it won't call the endpoint. Instead you ask for a certain entity type name.
You can map an entityType to a resourcename using the setEntityTypeForResourceName function:
metadataStore.setEntityTypeForResourceName('Speakers', 'Person');
See chapter "Resources names are not EntityType names" and the following chapters here: http://www.getbreezenow.com/documentation/querying-locally

Why breeze sends to server originalvaluesMap for unmapped property?

I'm using breeze 1.4.11 with Web Api and EFContextProvider
on client after metadata fetched I extend entity type:
var addressStringProperty = new breeze.DataProperty({
name: "addressString",
isUnmapped: true
})
metadataStore.getEntityType('Account').addProperty(addressStringProperty);
this property is computed and used only on client
after entitymanager.saveChanges([accountEntity]) I see on server side in contextprovider.BeforeSaveEntity that entityInfo.OriginalvaluesMap contains Key "AddressString" with Value == null.
same thing with extending entity like:
var accountCtor = function() {
this.addressString = ko.observable()
};
metadataStore.registerEntityTypeCtor('Account', accountCtor);
How to omit this behaviour?
It's a good question and probably an oversight. I'll add it as a new bug, but... just out of curiousity, why is this problematic for you?

how to get an unsaved entity on server but not for saving?

i need to send my unsaved entity from the client to the server but not for saving changes
but inorder to do a process using the data on the entity and then change some of it's values and pass it back to the client
is this possible?
if not what are my options?
i tried to export the entity and then send it to a method on the webapi controller that gets a JObject but didn't find a way to deserialize it to the server entity
We did have a similar problem and found a solution as follows:
You need to take into consideration the way breeze manages it's objects.
1.Create custom saveBundle.
Consider complex order object.You need to fill your save bundle with each nested object inside order.
Like:
var saveBundle = new Array();
saveBundle.push(order.SaleAccountingInfo);
saveBundle.push(order.CostAccountingInfo);
saveBundle.push(order);
2.Create custom save options, where you can point to your custom Save Method on server
Like:
var so = new breeze.SaveOptions({ resourceName: "BookOrder" });
3.Call standard breeze function and pass it created params
manager.saveChanges(saveBundle, so).fail(function () {
// manager.rejectChanges();TODO check what needed
deferred.resolve(true);
});
On server you need to have you custom function ready and hook some breeze delegates
[HttpPost]
public SaveResult BookOrder(JObject orderBundle)
{
context.BeforeSaveEntityDelegate = OrderBeforeSaveEntity;
context.BeforeSaveEntitiesDelegate = SaveOrder;
context.AfterSaveEntitiesDelegate = BookOrderAfterSave;
try
{
return context.SaveChanges(orderBundle);
}
catch (Exception)
{
throw;
}
}
You can a lot of stuff in first two delegates but it is the last one you are looking for
private void BookOrderAfterSave(Dictionary<Type, List<EntityInfo>> orderSaveMap, List<KeyMapping> orderKeyMappings)
{
var orderEntity = orderSaveMap.Where(c => c.Key == typeof(BL.Orders.Order)).Select(d => d.Value).SingleOrDefault();
BL.Orders.Order order = (BL.Orders.Order)orderEntity[0].Entity; //your entity
//logic here
}
Hope it points to right direction.
we are doing something similar here. it'll save the entity so i'm not sure if this fits your question.
you can do:
entity.entityAspect.setModified()
then issue a saveChange()
then you can do your calculations on the server.
in our case we are using breeze.webapi so we are doing this in the beforeSave(entity) method.
breeze by design sends the changed entity then back to the client where the cache gets updated with your changes done on the server.

Resources