Send Update as merge call on deep entity in UI5 - odata

I have a deep entity like below I am reading the data from
GET Entity(guid'018162718')?$expand=Detail
I have to send update call on this entity such that
its POST Entity
MERGE Detail
I tried oModel dot create() but its not updating the navigation item.
Also oModel dot update() is triggering PUT by default

Related

How can I move data between different controllers in rails while also receiving user input?

I'm relatively new to rails, and trying to write a program that involves querying data (a user_id and list of options) from an external API in one controller (Controller A), pushing the options to a view such that a user can select the option they want (via <select_tag>, and then sending both the user_id and selected_option to a different controller (Controller B).
I use an event listener to send an ajax post request to Controller B when an item is selected from my dropdown list. The issue is that the ajax request creates a new class instance, where in reality I want to reference the instance of Controller B that I instantiated in Controller A.
How I can get both the user_id and selected_option into Controller B based on the architecture I have described?

How to prevent OData service call on model change

I have a sap.m.Table whose "items" are bound to oData v2 model. I need to delete item on click on delete icon. Here is what I do:
On click of delete icon, I get all the rows in the model, delete the one in question and set the property of model again.
However since the model is changed, it triggers a backend round trip and brings the latest data and table shows the original rows again.
I tried setting binding mode to OneTime but that does not work. Also tried setting RefreshAfterChange to false but even then service was called again.
Here is my code -
Controller
onInit: function() {
var oModel = new sap.ui.model.odata.v2.ODataModel("url", {
json: true,
useBatch : false,
refreshAfterChange: false,
defaultBindingMode: "OneTime"
});
this.getView.().setModel(oModel, "model1");
},
onDeleteIconPress : function(oEvent) {
// get the selected row
// get all the rows in oOriginalRows
// loop over oOriginalRows and delete the selected row from it
// set the model to reformed oOriginalRows
this.getView().getModel("omodel1").setProperty("/", oOriginalRows);
// Till this point every thing looks fine. I can see changes in the model
// refresh is called automatically and data service triggers backend call
// This fetches original data again and table shows all data again
}
How can I not trigger the round trip again? I need to update the locally
Your approach won't work with a ODataModel as it is strictly server side. Please use the corresponding remove method to delete an entity from the server.
Since Odata is server side model, it always triggered a round trip. So I did not bind my sap.m.Table to Data model. Instead I triggered a read manually. On success I copied the data received to local JSON model. I bound my table items to this JSON model. Now the delete button works just fine.
// Define a JSON Model
oJsonModel = new sap.ui.model.json.JSONModel();
//oModel is Odata model defined in manifest file
oModel.read("/entity1", {
success: function(oData, oResponse){
oJsonModel.setProperty("/entity1", oData.results);
// bind oJsonModel to table here
}
}

Is there an event in Breezejs that is fired when an entity is added or deleted?

I have looked around for an answer to this, but I have come up dry so far. What I would like to do is have an event handler in a specific view model that listens to Breeze for entities being added or deleted so the view model can take appropriate action on the array it is managing. Does such an event exist?
I have a Jobs view model for my Jobs view that contains, among other properties, a ko.observableArray of Job entities, and a NewJob view model for my NewJob view. Both view models share the same data service. I would like to simply use the DataService from the NewJob view model to insert a new Job entity in to Breeze, and then have the Jobs view model simply subscribed to an event so it would know to add the new Job into it's Jobs array.
TIA
The Breeze EntityManager has an entityChanged event that may be used like so:
var em = new EntityManager( {serviceName: "api/NorthwindIBModel" });
em.entityChanged.subscribe(function(changeArgs) {
// This code will be executed any time any entity within the entityManager is added,
// modified, deleted or detached for any reason.
var action = changeArgs.entityAction;
var entity = changeArgs.entity;
// .. do something to this entity when it is changed.
});

PartialView updating ModelData?

am new to MVC but have a Q about PartialViews. I have two PartialViews accessing Model Data from the ParentView (Model Data passed through via the ParentView's Controller). The first PartialView is used to update (add/delete values) Model Data to the database. The second PartialView generates a document based on the ParentsView Model Data. The problem is that if the data is changed in the database by the first PartialView then the ParentViews Model Data is now out of date and hence the Second PartialView (referencing the ParentsView Model Data) is now working with out of date data.
I realise the above should be re-engineered to to better suite, however is there a way to make the updated Model Data available at the ParentView level for the second PartialView to reference?
Usually in order to update something into the database an HTTP request is sent to the server and the controller action performs the update and renders a view meaning that the whole page is reloaded and the model data updated.
If you perform an AJAX request to update the database then you might need to update the second partial view as well so that changes are taken into account.

Can you reuse a LINQ datacontext in an ASP.NET MVC application with strongly typed views

I am somewhat confused on how to properly use LINQ in combination with an ASP.NET MVC strongly typed view.
My situation is as followed.
1) I retrieve a single object through LINQ
2) I pass this LINQ object to a strongly typed view. (edit form)
3) The user submits the form and the controller receives the LINQ object.
So my questions are:
1) Is the object the controller method receives after the submit still tight to the original datacontext or is it a newly created instance?
2) What is the preferred way to store the updated values in the database using LINQ. If it is still tight to the original datacontext a simple call to SubmitChanges() would be sufficient. But how to maintain the datacontext?
I would like to be able to save these data objects without having to use really ugly linq update statements. (Like retrieving the row again and manually update its values)
Any help, insights and preferably code samples would be appreciated.
Regards,
Dennis
You should fetch the existing object again, and update it, somthing like:
public ActionResult Edit(int ID)
{
DataEntity entity = _service.GetMyEntity(ID);
UpdateModel(entity);
//Saving code goes here.
return View();
}
The entity that you are talking about retrieving is no longer attached to the data context.
Is the object the controller method
receives after the submit still tight
to the original datacontext or is it a
newly created instance?
It won't be attached to a DataContext at all on Submit.
What is the preferred way to store the
updated values in the database using
LINQ. If it is still tight to the
original datacontext a simple call to
SubmitChanges() would be sufficient.
But how to maintain the datacontext?
The preferred way is to create a new DataContext, retreive the old object from the database, update the fields based on what was submitted to your Form, and then save the updated copy from the new Context.
Use your linq objects to get the data and put it into a POCO (plain old c# object) Model specifically for your view.
Your linq2sql/ef objects can be structured to store data which do not necessarily reflect your views
Your model can be updated and validated by the MVC framework by placing it in the call to the post.
public ActionResult EditPost(EditModel model)
When you have a valid model you can then transfer the data back to the database via your linq2sql/ef objects
if(ModelState.IsValid) // save to db
Create a DataContext when you need to load and save, don't persist it
1) It is a new instance (through model binding feature) and it is not attached.
2) It depends, but the best would be probably new instance using(var context = new DBContext()) etc.
3) The most simple thing is fetch the object, pass in the updated values and SubmitChanges() (as you describe). Other option is described in this article. You create new instance of the object (or you have it from the model binding), attach it to the context and submit changes:
public ActionResult Test(MyModel model)
{
DbContext.Models.Attach(model);
DbContext.Refresh(RefreshMode.KeepCurrentValues, model);
DbContext.SubmitChanges();
}

Resources