gzost
gzost

Reputation: 2445

How do I pass parts of my Knockout viewmodel to a component when loading everything via require.js?

I'm getting started with Knockout.js components. I need to pass a component a part of my viewmodel as its data to use.

When the viewmodel is global, this is simple

<my-component params="myGlobalViewModel.dataSubSet"></my-component>

How do I do this when the viewmodel is loaded via require.js, and there is no access to this from global scope?

At the moment I additionally store a reference to the viewmodel in a global variable and then use this. Is there a better way?

Upvotes: 0

Views: 34

Answers (1)

Roy J
Roy J

Reputation: 43881

Your viewmodel is made "global" (from the HTML bindings' perspective) when you call ko.applyBindings() on it. If dataSubSet is a member of your viewmodel, just do

<my-component params="dataSubSet"></my-component>

Upvotes: 1

Related Questions