#1 2017-06-14 15:24:38

svanneste
Member
From: Paris, France
Registered: 2015-05-27
Posts: 155

[SOLVED] Retrieve the properties of Object ?

Hello David and other pals,
Perhaps I am not seing it but, do we have a way to retrieve the properties of an unknown object ?

For example : in the case of using only one HttpClient for many Views, instead of adding the data values from inside each view, I do create an object. I have thought to submit that object to an AB function which would iterate each property and put it with its value as the HttpClient.
To be able to iterate, I add the properties in a separate array in the current View. And in the function, read each item and get its value from the Object.

This works great but is a bit long when using the same mechanism from the HttpClient.Response with an unkown Object (for ex: read an object property named "keys", split it by comma, iterate the array, get the property, get the value, get the property named "subkeys", split it, etc.).

Perhaps is there a better way to achieve this ? Many thanks

Edit: for example Object.GetProperties ?

Last edited by svanneste (2017-06-14 18:32:33)

Offline

#2 2017-06-14 17:19:30

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,661
Website

Re: [SOLVED] Retrieve the properties of Object ?

Hello Samuel,

First of all, it's no problem to add as many HttpClient (and app's views) as you needed in an application, so, maybe it's a good idea to do not use the same HttpClient for different purposes, so we are more sure about how to deal with the specifics HttpClient possible responses, etc.

About exactly what you wanted, that is, iterate over an Object variable, maybe we can't do it using AB actions right now, but we always can use pure Javascript code or certain Javascript library like jQuery if we wanted to use it.

In pure Javascript we can do something like this:

for (var property in object) {
    if (object.hasOwnProperty(property)) {
        // do stuff
    }
}

Where "object" is our variable in this case. Look at this tutorial to know how we can access app's variables and other stuff from pure Javascript code. Please, post here any further question about this question.

Offline

#3 2017-06-14 17:30:47

svanneste
Member
From: Paris, France
Registered: 2015-05-27
Posts: 155

Re: [SOLVED] Retrieve the properties of Object ?

Thank you David. I will try to apply that.

Please, don't take it bad, it is not an aggressive question but only a surprised one : which such projects you have previously described (20 views or more), you don't create some functions to reuse components ?
I am probably too object oriented now (reason why I'm often lost here crying) but isn't the fact to create each view as a "small" individual app, a "loss of time" ? You are more experienced than me, so I will try to apply that way to compare

Offline

#4 2017-06-14 17:34:43

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,661
Website

Re: [SOLVED] Retrieve the properties of Object ?

Hello Samuel,

First of all, some sample code to try it:

NewObject "[MyObject]"
ObjectSetProp "[MyObject]" "Name" "App"
ObjectSetProp "[MyObject]" "LastName" "Builder"

StartJS
  for (var property in window.App.RootScope.MyObject) {
    if (window.App.RootScope.MyObject.hasOwnProperty(property)) {
      alert(property + ': ' + window.App.RootScope.MyObject[property]);
    }
  }
EndJS

About your last question... certainly we can reuse some functions and non visual controls across app's views, however, here in this thread you are talking about to reuse an HttpClient to perform various HTTP calls... ok, can be good... but my idea is that we have no limits in the number of HttpClients controls we can use, so, maybe it's a good idea to maintain it separated.

Of course, if the HTTP calls differs in a couple of arguments... maybe it's a good idea to simply reuse a control, however, I am thinking (that's the case of the application in which I am working now) that every HttpClient control have a very particular mission in the application: someone list certain data, others list other information, this list users, this other list articles, this other can be used to delete stuff, etc., etc.

Different tasks -> differents URLs (server's scripts) -> differents HttpClients controls. smile

However, continue talking about the application I am workin on right now, we reuse the same WebSocket control to maintain two different chats. In this case it's better to reuse the same control, which is placed in the first app's view and can be used in others too (since they are a non visual control).

Why we consider (may others can think different!) to reuse the WebSocket controls is fine? Because we maintain the connection in all the user's session, so we do this on one Websocket server and not two. On the other hand, the same WebSocket control's Message event is capable to differenciate the two available kind of chats. For this reasons we reuse this specific control.

But on the other hand we use lot of HttpClients controls... in different app's views... which produces differents results.

Offline

#5 2017-06-14 18:30:34

svanneste
Member
From: Paris, France
Registered: 2015-05-27
Posts: 155

Re: [SOLVED] Retrieve the properties of Object ?

Fantastic App Builder smile Thanks David

NewObject "[MyObject1]"
ObjectSetProp "[MyObject1]" "LastName" "Builder"
objectsetprop "[MyObject1]" "is" "GREAT"
NewArray "[ObjectProperties]"

//Gets all keys (properties) of an Object
StartJS
window.App.RootScope.ObjectProperties = Object.getOwnPropertyNames(window.App.RootScope.MyObject1);
EndJS


ArrayLength "[ObjectProperties]" "[ObjectPropertiesLength]"
dec "[ObjectPropertiesLength]"

Loop "0" "[ObjectPropertiesLength]" "[l]"
  ArrayGetItem "[ObjectProperties]" "[l]" "[theKey]"
  ObjectGetProp "[MyObject1]" "[theKey]" "[theValue]"
  Console "[theKey] = [theValue]" "info"
EndLoop

Offline

#6 2017-06-14 18:32:42

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,661
Website

Re: [SOLVED] Retrieve the properties of Object ?

Hello Samuel,

Glad to know you learn something more. Certainly it's a good think we can extend App Builder using pure Javascript. Our application's actions are finally translated into Javascript code, and, the compiler is prepared to accept Javascript code directly (with the StartJS and EndJS actions) so it's easy to mix App Builder's actions and Javascript code. You shown above a good example of that.

Offline

Board footer

Powered by FluxBB


Copyright ©2018 DecSoft. All rights reserved.