Suppose I wanted to have a text input where a user would type a product ID and press enter to load the product data into a view. I only want to load / view one product at a time.
I setup an event handler for my text input which gets the typed in product ID and then calls the read method for the odata model:
onSubmit: function(){
var prodID = sap.ui.getCore().byId("product_id").getValue();
oData.read("/Products('" + prodID + "')", {
success: function(){
console.log(oData.getProperty("/Products('1')/Name")); // Outputs the product name
},
error: function(){}
});
},
This successfully loads the data into the model. However, the model is "keyed" (for lack of a better term) by /Products('1') meaning that to access the data, you must know the product ID.
Question
Where do I store the current product ID so that subsequent views know how to access the loaded product name?
I currently have a separate JSON model defined where I am storing the active product ID. In my subsequent view's onBeforeRendering, I am reading that active product ID and then creating a new context, but this doesn't seem right.
var context = new sap.ui.model.Context(myModel, "/Products(1)");
this.getView().setBindingContext(context, "products");