Skip to content

NavigationEntry.bindingContext of app.run() not bind to rootView #6835

Closed

Description

@webleaf

Environment

  • CLI: 5.1.1
  • Cross-platform modules: 5.1.2
  • Android Runtime: 5.1.0
  • iOS Runtime:
  • Plugin(s):

Expected behavior
Root View Text should appear. onLoaded method should be called.

To Reproduce

Just replace your app.ts with this:

import * as app from "tns-core-modules/application";
import {parse} from "tns-core-modules/ui/builder";
import {StackLayout} from "tns-core-modules/ui/layouts/stack-layout";
import {Observable} from "tns-core-modules/data/observable";

class RootLayoutViewModel extends Observable {

    public rootViewText = "Root View Text";

    onLoaded() {
        console.log("RootLayoutViewModel.onLoaded()")
    }
}

const rootLayoutXml = `<StackLayout loaded="{{ onLoaded }}"><Label text="{{ rootViewText }}"/></StackLayout>`;
const rootLayoutView = <StackLayout>parse(rootLayoutXml);
const rootLayoutViewModel = new RootLayoutViewModel();

app.run({
    bindingContext: rootLayoutViewModel, // <--------------- this is NOT do what expected
    create: () => {
        // rootLayoutView.bindingContext = rootLayoutViewModel; // <---------- workaround
        return rootLayoutView;
    }
});

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Activity

  1. changed the title [-]NavigationEntry.bindingContext not binded do dynamically created view[/-] [+]NavigationEntry.bindingContext of app.run() not bind to rootView[/+] on Jan 27, 2019
  2. self-assigned this
    on Jan 28, 2019
  3. tsonevn commented on Jan 29, 2019

    @tsonevn
    Contributor

    Hi @webleaf,
    Can you provide some more info about your use case(Why you need to set up the bindingContext on run) and what you are trying to achieve as a final result?

    Thank you in advance for your cooperation.

  4. webleaf commented on Jan 29, 2019

    @webleaf
    Author

    @tsonevn use case same as with using bindingContext in navigate()
    I use one ViewModel per page. It's not comfortable to me to have 3 files (xml, code-behind and view-model). Also it is not comfortable to me to store view in xml file. I need to store view as string.
    So there is no sense for me to use code-behind, because all I do in it is set bindindContext. All other things I do in ViewModel.
    By the way, same concerns modal windows, where it is generally not possible to specify bindingContext in showModal()
    Finally, app.run() have NavigationEntry as attribute, so it should work.

  5. webleaf commented on Feb 7, 2019

    @webleaf
    Author

    @tsonevn I gave you more info. What's next?

  6. NickIliev commented on Feb 20, 2019

    @NickIliev
    Contributor

    @webleaf the thing is that with application.run your root view should be Frame so the binding context is passed to the Frame and no to your initial page.

    If you want to start the app directly from a page (that would mean SPA app) you can use the start method

    Using the start method with binding context

    const vm = new Observable();
    vm.set("name", "John SMith");
    
    app.start({
        bindingContext: vm,
        create: () => { 
            return pageView;
        }
    })

    Apart from that if you want to have a Frame and more complex navigation scenarios then use the run and create a frame as root. The pass the binding context when you are creating the pages.

  7. webleaf commented on Feb 20, 2019

    @webleaf
    Author

    @NickIliev you are not right. With application.run your root view CAN BE any View NOT ONLY Frame. Vice versa, start method implicitly create Frame. I don't need such root Frame, because I use RadSideDrawer as root view.

    Note: Prior to version 4.0.0 all NativeScript application had single topmost Frame implicitly created by the application.start() method. With NativeScript 4.x.x and above the root Frame is no longer implicitly created. Instead you can specify any View to be the root of your application while using application.run({ moduleName: "app-root"}) where app-root is the file containing your root View.

    Note from here: https://docs.nativescript.org/core-concepts/application-lifecycle#application-run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions