In my SAPUI5 app I'm using odata v4 model to communicate with the server. I have a detail view that binds to an existing object something like this:
// Controller code:
this.getView().bindElement({
path: "/Objects(" + this.m_ObjectId + ")",
model: "modelName"
});
If I want to create a new item in that view instead, I'm not binding to an existing item of course. From the documentation I saw that one should create new items like this:
var oModel = this.getModel("modelName");
var oListBinding = oModel.bindList('/Objects');
var oNewKap = oListBinding.create({
'OBJECT_ID': 0,
'SOME_PROP': 'Test'
});
The question now his, how to bind that newly created (transient) object to my view? If I do a
this.getView().bindElement({
path: "/Objects(0)",
model: "modelName"
});
The app will try to fetch an existing item from the server again...
Any help is appreciated!
You may do this.getView().setBindingContext(oNewKap)
Related
In Master controller, on selection of item in the list, I am calling the detail view and binding the model data.
Master Controller:
itemSelected: function(result)(
var app = sap.ui.getCore().byId("appId");
var list = sap.ui.getCore().byId("mainListId");
var selectedItem = list.getSelectedItem();
var selectedPath = selectedItem.oBindingContexts.MainList.sPath;
var Item = sap.ui.getCore().getModel("MainList").getProperty(selectedPath);
var endPoint = Item.Endpoint;
console.log(endPoint);
var oModel =new sap.ui.model.json.JSONModel(Item);
sap.ui.getCore().setModel(oModel,'DetailItem');
app.toDetail('idDetail','slide')
},
Detail View:
I am able to bind the model data here in oTable.
oTable.bindRows("DetailItem>/");
1.How store the model data in a variable? (tried: sap.ui.core().getModel(modelname).getData() and similar others. This gives undefined, as it gets read even before the ItemSelected function in the Master controller is called.)
How to bind/get the model data into Detail controller after the itemSelected function is called?
Thanks!
You can set the model to i.e. a view by using the following statement:
this.getView().setModel(oModel, "modelID");
I'm working on a sap fiori Master-Detail application on sap web ide.
i want to filter a table i have on detail view based on an ID i will get from a list in master view.The table's detail view is located in a fragment. I created a js function to filter that table in the detail controller but i can't seem to have the id of the table in the fragment.
Here's my detail-controller's code:
this._oView = this.getView();
this._oView.attachAfterRendering(function() {
var sPath1 = "Qmnum";
var sOperator1 = "EQ";
var sValue1 = "10000000";
var oFilter1 = new sap.ui.model.Filter(sPath1, sOperator1, sValue1);
var oBinding = this.byId("tableid").getBinding("items");
oBinding.filter(oFilter1);
});
i tried using also this
sap.ui.getCore().byId();
&
this.getView().byId();
but still undefined.
If XML fragments are used within XML views, giving the view ID as
fragment ID will allow calling this.byId(…) in the view’s controller
to retrieve controls inside the fragment.
var oFragment = sap.ui.xmlfragment(this.getView().getId(), "my.useful.VerySimpleUiPart" );
Initialize the fragment and attach it to the current view:
// Init fragment
var oFragment = sap.ui.xmlfragment("herePutJourneyToFragment.fragmentName", this.getView().getController());
// Attach the fragment to the current view
this.oView.addDependent(oFragment);
After that you should be able to find the table in you view:
var oTable = this.byId("tableid");
EDITED 13:45 150317 bellow:
Tutorial.
I am binding a SELECT HTML tag with some dynamic values using knockout JS. Additionally, i am trying to set a selected choice which is failing. Please suggest where i am going wrong.
self.level1Choices.selectedChoice = ko.observable(2); - this line does not seem to work.
The JSFiddle for this code is at http://jsfiddle.net/oarp7gwj/7/
The dropdown is not loading in the JSFiddle for some reason. I dont think i have referenced the knockout JS correctly. In my local environment, I am able to load the select box with the values. However, i am not able to set the selected value.
#Wayne Ellery, #QBM5 - please advise since you know about this already :)
You should use var to declare your model object to avoid scoping issues
var viewModel = new DataModel();
The main issue was you need to add to the Datamodel by exposing it through the this variable in the Datamodel.
var DataModel = function (client) {
var self = this;
self.level1Choices = ko.observableArray();
};
Take a look at the Helo World example as to how to do this:
http://knockoutjs.com/examples/helloWorld.html
I've scoped this to self as it's a best practice to not worry about this referring to something else mentioned here: http://knockoutjs.com/documentation/computedObservables.html.
I moved the loadAllApprovers method inside the DataModel as this is where it belongs and so that it has access to populate the datamodel.
I added the mobile services client to the constructor so that it can be mocked for testing your model.
var DataModel = function (client) {
var self = this;
self.level1Choices = ko.observableArray();
var loadAllApprovers = function () {
var allAppprovers = client.getTable('TABLE');
var query = allAppprovers.select("ID", "FirstName").read().done(function (approverResults) {
self.level1Choices(approverResults);
}, function (err) {
console.log("Error: " + err);
});
};
loadAllApprovers();
};
You were also missing knockout in your jsfiddle.
http://jsfiddle.net/az4rox0q/6/
I would like to have users insert and edit information about entities on the same page in a similar fashion to google alerts: http://www.google.com/alerts/manage
Any advice on how this could be achieved?
It is possible changing the routing in the method RegisterRoutes in Global.asax as indicated in the remarks section present in the same file.
Enabling this section:
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
Action = PageAction.List,
ViewName = "ListDetails",
Model = DefaultModel
});
instead of the already enabled:
routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
{
Constraints = new RouteValueDictionary(new { action = List|Details|Edit|Insert" }), Model = DefaultModel
});
It's also possible to enable custom routes for particular tables.
I have the following code in my post action method for Edit.
JobCardService.Update(viewData.JobCard);
var js = new JavaScriptSerializer();
ViewData["Notifications"] = js.Serialize(new {NoteificationType = "Success", Message = "The installtion was successfully updated"});
return RedirectToAction("Index");
However, on the client, ViewData is null/empty, i.e. this client code
var notifications = eval("<%= ViewData["Notifications"]%>");
renders as
var notifications = eval("");
I'm sure I'm doing something small wrong.
ProfK - I think (as you'll no doubt be aware) you'll have to parse that json result in javascript once you get into your index view via the redirect. the jquery .getJson() method would seem most appropriate: http://api.jquery.com/jQuery.getJSON/
Also, as you're doing a RedirectToAction, then the context of the ViewData will be lost. In that case, you want to use TempData as a drop in replacement. Below is an example of what you could try:
jim
[edit] - not sure if this would work:
// in the controller
TempData["Notifications"] = js.Serialize(...);
// in the index view
function getMyJsondata() {
var json = $.getJson('<%=ViewContext.TempData["Notifications"] %>');
}
or as per your amendment to the question, try this:
// alternative in index view
eval("(" + "<%= TempData['Notifications']%>" + ")");
give it a go...
adendum:
to quote from a previous SO question on Tempdata vs ViewData: What is TempData collection used for in asp.net MVC?
TempData is used to share data between
controller actions. If your controller
does a RedirectToAction and the target
action needs data (perhaps a
particular model instance) to act
upon, you can store this data in
TempData. Using TempData is similar to
storing it in the session, but only
for one round-trip. You use TempData
when you need to pass data to another
controller action rather than a view
for rendering.