Tutorial: Platform.createView

Platform.createView

Creates a new view and attaches it to a specified target window. If the view already exists, will reparent the view to the new target. You do not need to set a name for a View. Views that are not passed a name get a randomly generated one.

Basic example

let windowIdentity;
if (fin.me.isWindow) {
    windowIdentity = fin.me.identity;
} else if (fin.me.isView) {
    windowIdentity = (await fin.me.getCurrentWindow()).identity;
} else {
    throw new Error('Not running in a platform View or Window');
}

const platform = fin.Platform.getCurrentSync();

platform.createView({
    name: 'test_view',
    url: 'https://developers.openfin.co/docs/platform-api'
}, windowIdentity).then(console.log);

Reparenting a view

let windowIdentity;
if (fin.me.isWindow) {
    windowIdentity = fin.me.identity;
} else if (fin.me.isView) {
    windowIdentity = (await fin.me.getCurrentWindow()).identity;
} else {
    throw new Error('Not running in a platform View or Window');
}

let platform = fin.Platform.getCurrentSync();
let viewOptions = {
    name: 'example_view',
    url: 'https://example.com'
};
// a new view will now show in the current window
await platform.createView(viewOptions, windowIdentity);

const view = fin.View.wrapSync({ uuid: windowIdentity.uuid, name: 'yahoo_view' });
// reparent `example_view` when a view in the new window is shown
view.on('shown', async () => {
    let viewIdentity = { uuid: windowIdentity.uuid, name: 'example_view'};
    let target = {uuid: windowIdentity.uuid, name: 'test_win'};
    platform.createView(viewOptions, target);
});

// create a new window
await platform.createWindow({
    name: "test_win",
    layout: {
        content: [
            {
                type: 'stack',
                content: [
                    {
                        type: 'component',
                        componentName: 'view',
                        componentState: {
                            name: 'yahoo_view',
                            url: 'https://yahoo.com'
                        }
                    }
                ]
            }
        ]
    }
}).then(console.log);