I try to access the value of my json objects in sap ui5, but the getproperty function cannot access the required data.
But, at first, I have created a xsodata file with some service definitions, e.g.
"CUSTOMER_ATTR_G3" as "Customers";
Then I try to get these data in view.js file with the following code:
oModel.loadData("UserInterface_G3/SERVICES/CUSTOMER_ATTR_G3.xsodata/Customers?$select=CUSTOMER_ID,CUSTOMER_DESCRIPTION&$format=json");
When I am using console.log(oModel) I see in the odata section that the values are in the object but I cannot access to them. The following screenshot should show the structure of the object:
I tried for instance:
console.log(oModel.getProperty('/CUSTOMER_DESCRIPTION'));
or
console.log(oModel.getProperty('results/CUSTOMER_DESCRIPTION'));
But I cannot access the values of the object.
Does anybody have an idea on that?
console.log(oModel.getProperty('results/CUSTOMER_DESCRIPTION'));
You need to access your Property like that:
console.log(oModel.getProperty('d/results/0/CUSTOMER_DESCRIPTION'));
You forgot the position inside of your Array. Your Path needs the position, so if you want to get the first entry CS_0001 then you have to write result/0/CUSTOMER_DESCRIPTION.
EDIT:
Actually it depends on your Model, how you have to access the Property. Can you pls show me how you defined your oModel?
Related
Ive looked related posts and couldn't quite find what I was looking for.
So I am build a backend rest api and I have certain tests I am collecting data on. The tests have their own models, and these models are associated with collections (obviously).
So I have a separate controller for each model. Now I have a "job" controller which queries data from each separate test. Now I have a separate script where I store these model objects in an JSON object. I am wondering how I can access these models properly (I am close but cant quite assign properly). Here is the block:
const testMappings = {
'aprobe':aprobe,
'status':status,
//'rxserial':rxserial,
}
Now when I try assignment as follows, where testMappings is the imported script variable:
const testMappings = activeTests.testMappings;
console.log(testMappings['aprobe']);
I get the following output:
Model {aprobe}
I would like to access the actual aprobe object. Also if anyone knows a better way of dynamically assigning these (instead of having bunch of if statements ie if(name == 'aprobe').... do something), it would be much appreciated.
You are probably looking for something like below :
const name = 'aprobe';
Object.keys(testMappings).indexOf(name) > -1 ? testMappings[name] : null
the above should give you: Model {aprobe}
So basically if the key exists in your object then you'd like to fetch the value of that key which would give you your model dynamically.
I have 1 main component. Inside of this component i use WebSockets to receive data from the server. I receive an object with a range of fields inside it. The number of fileds can be from 0 till 10, for example. Each time i receive an object i use forEach() to take all fields. For each field i need to init a component, like this:
self.dcl.loadIntoLocation(StreamComponent, self.elementRef, 'stream');
If a copy of a component for current field of an object already exists, i need to update it with new received data within the view. The main my problem is i don't know how to pass the data from WebSockets to created component. I can create and init it, but i never mind how to pass data to it. Any ideas ?
You could try to leverage the promise returned by the method:
self.dcl.loadIntoLocation(
StreamComponent, self.elementRef, 'stream').then((compRef:ComponentRef) => {
compRef.instance.someAttr = something;
});
This promise allows you to get the instance of the newly created component and set data in its properties (for example, the web socket).
I am building an Ember component and it's being passed a model
{{ratings-summary-comp model=model}}
So you can call this model inside an action on the component's js file, like this:
this.get('model')
When I log this in the browser console, it outputs some kind of class:
Class {id: "1", store: Class, container: Container, _internalModel: InternalModel, currentState: Object…}
I want to convert this to a normal JS-object consisting of the model's attributes, so I can call the attributes like this:
model["attribute"]
This is so I can push a whitelist of attributes to an arrary inside the action.
How?
The model is just another Ember Object. You access its properties with get, as in
this.get('model').get(attribute)
And in the case of multiple attributes you can use getProperties:
this.get('model').getProperties('firstAttribute', 'secondAttribute', ....);
If you just need an array of attribute names, you can use Object.keys as suggested in this post on the ember github issues pages.
Running Object.keys(this.get("model")) will give you an array of attribute names.
Here's a JSBin example
Edit - The post uses Ember.keys, but that is now deprecated in favor of Object.keys.
I'm using tree with dijit.tree.ForestStoreModel as a model and JsonRestStore as a store.
It works fine when tree is initialised. After that I want to add another item to the store manually (not from server) to update the tree.
My question is how can I do it without posting the item to server by rest, just to local storage. I used store.newItem() but without success.
I think in this situation, you probably wouldn't want to actually use a JsonRestStore. Depending on your use case, you may want to use some other type of dojo store like Memory and on initialization of your Tree, load the data from your server through some other method.
Finally I was able to figure out what fired requests to server. That was jsonRestStore.save(), that commits dirty data to server.
After I've removed it from my code, I use the following to add new data to tree:
jsonRestStore.fetchItemByIdentity({
identity: parent.path,
onItem: function (parent) {
store_cloud.newItem(child, {
parent: parent,
attribute: 'children'
});
}
});
Here I have a child node and parent one. Also 'children' attribute is used for referencing.
In my AngularJS project, I am not able to share data of copied objects across different controllers.
I am able to share data properly if I set the properties in code but if I copy the object then it does not appear to work.
In the code block at the JSFiddle, you will see that I have two objects User and OriginalUser.
As soon as the FirstCtrl is loaded, I copy the User object to the OriginalUser object.
You will see that while the value assigned to User object in FirstCtrlis accessible in SecondCtrl but the data of OriginalUser is accessible only in the FirstCtrl.
You can see the code in action at Code Sample at JSFiddle
This is because you are not update OriginalUser data but you create a new variable using angular.copy function.
What you are looking for is angular.extend which will copy user properties to your original user. Now, both controllers use the same object instance.
Your first controller:
function FirstCtrl($scope,User, OriginalUser){
$scope.user = User;
$scope.o_user = angular.extend(OriginalUser, $scope.user);
}
Your fiddle is up to date.