Creating a global state object with ReactJS.NET - reactjs.net

How can I create a global state object for all my components to reach into? Normally I would just put that object on the window, but on the server there's no window ofcouse.
I have multiple #Html.React("MyComponent", new {}) so on the top of my head I could pass those global variables into all of these instances as props. But is there a better way to do this, as the data would be the same for each component.
I have been trying to set a variable with this line (seen in another thread):
ReactEnvironment.Current.Execute("var globalState = { sharedUrl: 'https://....' }");
But inside my bundle I can't seem to pick up the globalState variable - it's just empty.
As a note, I'm not running this as a SPA

Related

set and get variables throughout moive - actionscript 3

I need to set various variables depending on what a user clicks. Those variables then need to be accessed by other movieclips.
The problem is that at the time of clicking, those "other movieclips" don't exist on the timeline yet.
What I have done is assign the variable to the parent movieclip, although I guess I could just as easily set them to the root. Then I would grab that value when the relevant clip is initiated.
// set - works
MovieClip(parent).aMovieClip.someVariable = myVal;
// get? - doesn't work
getVar = MovieClip(parent).aMovieClip.someVariable;
the above doesn't work - what is the correct way to get that value whenever I need it. I guess it's a case of getting and setting global variables
Answer copied from:
http://www.experts-exchange.com/Software/Photos_Graphics/Web_Graphics/Macromedia_Flash/Q_22997849.html
with AS3.0, concept of _global variables have been removed completely.
although, an external class can be used to act as exactly AS2.0 _global
if you implement it properly, you should get desired results. here are the details:
1.
create a AS3.0 class with name "glo.as", and save it in the same folder where FLA resides.
2.
Just copy paste the following actionscript in "glo.as" class file.
////////////
package
{
//Class will act as _global object of AS2
// Static variable "bal" of this class is going to store "varibales, instance's/object's referece to be used by any other class or displayObject/movieClip
// sample code to store a reference of a movieClip
//
// glo.bal.myMovie = this.new_mc;
//
//
public class glo
{
public static var bal:Object = new Object();
}
}
//////
3.
Now just create any global variable anywhere by just using "glo.bal" instead of "_global"
for example:
glo.bal.myGlobalVariable = 22;
it will be accessible everywhere in the scope of your FLA and other classes.
Credit
Aneesh Chopra
http://www.experts-exchange.com/M_3631521.html
You could use proxies to take care of the values while the objects are still inexistent. These would only need get notified when the MovieClips get accessible in order to amend them with the pending values.

QML JavaScript import is empty

I have a QML file that imports a JavaScript library:
import "qrc:/scripts/protobuf.js" as PB
This library modifies the 'global' object during setup. Simplified, the JS library is:
.pragma library
(function(global){
global.dcodeIO = global.dcodeIO || {};
global.dcodeIO.ProtoBuf = {}; // In reality, a complex object
})(this);
On Windows and Linux this works as expected; later in my QML file I write var ProtoBuf = PB.dcodeIO.ProtoBuf; and it finds the dcodeIO property added to the 'global' object and properly gives me the object I need.
However, on another platform, the same code does not work. I get an error that it cannot read property ProtoBuf of undefined. I add debugging lines to my QML and see:
console.log(PB.dcodeIO); //-> undefined
for (var k in PB) console.log(k,PB[k]); //-> (no enumerable properties logged)
Yet, the JavaScript code in the library is loaded and runs. Within the library if I add console.log(global.dcodeIO) after the line linked above I see [object Object].
What might the difference be? How can I determine why Qt is running my JavaScript file, but not successfully associating the global object with PB?
The points listed under JavaScript Environment Restrictions could be relevant here:
JavaScript code cannot modify the global object.
In QML, the global object is constant - existing properties cannot be modified or deleted, and no new properties may be created.
...
Any attempt to modify the global object - either implicitly or explicitly - will cause an exception. If uncaught, this will result in an warning being printed, that includes the file and line number of the offending code.
Although you said it's working on two platforms, just not a third, so perhaps your usage is correct and it's a bug.
There's also this point:
Global code is run in a reduced scope.
Though that point seems to be about accessing QML objects from the loaded script.
The documentation has always been unclear to me on this subject. http://doc.qt.io/qt-5/qjsengine.html#globalObject says:
Returns this engine's Global Object.
By default, the Global Object contains the built-in objects that are part of ECMA-262, such as Math, Date and String. Additionally, you can set properties of the Global Object to make your own extensions available to all script code. Non-local variables in script code will be created as properties of the Global Object, as well as local variables in global code.
Which seems to contradict the restriction listed above. There's also this:
Note: The globalObject() function cannot be used to modify the global object of a QQmlEngine. For more information about this, see JavaScript Environment Restrictions.
It appears that the problem was a bad build of 5.7.0-alpha. Upgrading to 5.7.0 release and rebuilding has removed this problem.

sizeof object varies with context

In Win CE 6.0, class TankObject is defined in a static library, C++ compiler is VS2008; target is ARM4 (/QRarch4).
An instance of class TankObject is constructed by a call of the form
*TankObject myTankObject = new TankObject(parm1, parm2 ...);
Last attribute declared in TankObject definition is an object pointer, and when an assignment is made to same, memory corruption of another dynamically allocated object occurs.
Step into constructor reveals that operator new is called with a size of 0x500. sizeof(TankObject) reveals two different values, depending on the context:
In instantiating context (the application), sizeof(TankObject) and sizeof(*myTankObject) is 0x500.
In context of the object itself (the constructor, or object methods), sizeof(TankObject) and sizeof(*this) is 0x508. The address of last declared attribute is 0x500, relative to object.
The call to new requests and receives 0x500 bytes. The object itself expects and uses 0x508 bytes, which can cause assignments to last attribute to step on other dynamically allocated objects.
Work around is to declare another unused attribute at the end of the object definition, to pad the request to new.
Both contexts include the same .h file, to debug I changed include statement to an explicit path name. I also went through compiler switches, made them identical. So I am puzzled, if any one can shed light, I'm glad, I would like to know a proper solution.

How to access children of mainWindow for UI Automation in Xcode

I have a structure in the mainWindow() of my application as follows:
Within the children directory of the UIAWindow near the top of the image, I am trying to access Item 1, where it is a UIAButton.
This structure is generated from
#import "tuneup/tuneup.js"
goAbout = function(target, app)
{
UIATarget.localTarget().frontMostApp().mainWindow().logElementTree();
};
test("go about", goAbout);
If I add .button() or [1] it raises an exception, so I cannot do something like so
...
UIATarget.localTarget().frontMostApp().mainWindow().buttons().logElementTree();
What can I do to access the object within the children subdirectory of the UIAWindow?
You can access that button by adding .buttons()[0]. The
UIATarget.localTarget().frontMostApp().mainWindow().buttons();
expression returns a javascript array (which has a 0 based indexing).
Here you can find a rather usable documentation:
https://developer.apple.com/library/ios/documentation/DeveloperTools/Reference/UIAutomationRef/UIAutomationRef.pdf
Anyway studying this plist is not the most convenient method to explore your object hierarchy. I prefer to check instruments output directy. (Or standard output, if you run instruments from command line.)
That tree hierarchy ended up not meaning anything useful to me, and I ended up utilizing target methods to access the elements of my window. Cheers!

Mfc multiple windows same document

I have a SDI MFC application which CMainFrame class is derived from CFrameWndEx.
The app uses the CSingleDocument template for connecting Document/View/Frame.
I need to have another window with the contents of a View related to the same document of the view of the Main Window.
Is it correct to have another instance of the CMainFrame as a secondary window?
After some approaches I came to a solution:
1st:
Have two CMainFrame instances on the App, where the second has a boolean field to indicate it is one or other. This boolean allowed me to skip the creation of everthing creation only on the second, because I only want it to be a rectangle with the view. I've added a special public constructor that to CMainFrame that always puts the referred boolean to true.
CMainFrame *pSecondFrame = new CMainFrame(TRUE /*dumb*/);
It worked, but it was very slow. The view of the first MainFrame was completely unusable due to its lack of speed.
2nd:
Have a CSecondFrame that is also derived from CFrameWndEx and has almost nothing on its body. As IDR_MAINFRAME has nothing, I can expect to do LoadFrame(IDR_MAINFRAME) and have an almost empty frame on the App.
CSecondFrame *pSecondFrame = new CSecondFrame();
It worked, but it was very slow. The view of the first MainFrame was completely unusable due to its lack of speed.
For both 1st and 2nd approaches, I had to add a new CreateContext, as I can not reuse the mainframe View:
if (!pSecondFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
CMainFrame* pMainFrame=(CMainFrame*)::AfxGetMainWnd();
if (!pMainFrame)
return FALSE;
CMyView* pView=(CMyView*)(pMainFrame->GetActiveView());
if (!pView)
return FALSE;
CCreateContext context;
context.m_pCurrentDoc=pMainFrame->GetActiveDocument();
context.m_pNewViewClass= RUNTIME_CLASS(CMyView);
context.m_pLastView= pView;
context.m_pCurrentFrame = pSecondFrame;
pSecondFrame->CreateView(&context);
3rd:
At the beginning of the InitInstance function I have the Document Template:
So lets play with its CreateNewFrame method:
POSITION pos= pDocTemplate->GetFirstDocPosition();
//pFrameTemp attribution
CFrameWnd* pSecondFrame= pDocTemplate->CreateNewFrame(pDocTemplate->GetNextDoc(pos),pFrameTemp);
I tried different things for the pFrameTemp: NULL, a CMainFrame, CSecondFrame. Everytime I got a frame that is carbon-copy of the main and I dont't want this.
For the CMainFrame and CSecondFrame cases I tried to explicity call their Create( ...) functions, which fail,so they don't even pass on their OnCreate(...) method.
Instead of this I also tried to do a LoadFrame(IDR_MAINFRAME) for both cases. Now I will have three frames: The Main, the Second, which is carbon-copy of the main, and the one created with LoadFrame, which is totally blank.
4th:
After seeing that he CreateNewFrame method creates a new Frame based on the frame it has on the template and not the one that is passed in its parameter, I decided to try create addicionally another template:
CSingleDocTemplate* pSecondDocTemplate;
pSecondDocTemplate2 = new CSingleDocTemplate(
MenuID,
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CSecondFrame),//only the frame differs
RUNTIME_CLASS(CMyView));
AddDocTemplate(pSecondDocTemplate);
POSITION pos= pDocTemplate->GetFirstDocPosition();
CFrameWnd* pSecondFrame=pSecondDocTemplate->CreateNewFrame(pDocTemplate->GetNextDoc(pos), NULL);
Notice that I am giving the doc of the first template as parameter, not the second, to CreateNewFrame.
This is the only solution I tested that works and doesn't slow down operations in the view.
It seems I need, for every solution I presented to do
pSecondFrame->ShowWindow(SW_SHOW);
for getting the window visible.
Have you considered adding an additional CView in a splitter window? This SO topic covers it, and has links to details.

Resources