Access Knockout component DOM from desired AMD viewModel


Silicon Valley

I load my Knockout components this way:

ko.components.register("example", {
    viewModel: {require: "widgets/example"},
    template:  {require: "text!widgets/example.html"}
});

with example.js(extremely simplified):

"use strict";

define(["knockout"], function(ko) {

    function ExampleWidgetViewModel(params) {

        this.editedText = ko.observable("Example");
    }

    return ExampleWidgetViewModel;
});

and example.html:

<div id="example-dlg", data-bind="text: editedText"></div>

The component is called as usual <example></example>and everything works fine. But I want to access the DOM to remove idthe need in the template. Try changing the method in the documentation to:example.js

"use strict";

define(["knockout"], function(ko) {

    function ExampleWidgetViewModel(params, componentInfo) {

        this.editedText = ko.observable("Example");
    }

    return {createViewModel: ExampleWidgetViewModel};
});

But it complains that it cannot be editedTextfound. Other variations have the same problem:

"use strict";

define(["knockout"], function(ko) {

    function creaExample(params, componentInfo) {
        let ExampleWidgetViewModel = (params) => {

             this.editedText = ko.observable("Example");
        }
        return ExampleWidgetViewModel;
    }
    return {createViewModel: creaExample};
});

Can you provide a working example? Thanks!

Silicon Valley

There's nothing better than asking for help finding a solution...I'm calling the viewmodel by mistake. The correct example.jsfile is:

"use strict";

define(["jquery", "knockout"], function($, ko) {

    function factory(params, componentInfo) {
        function ViewModel() {

            // To show how to access the component external div
            console.log($(componentInfo.element.firstChild).attr("id"));

            // To show it can correctly access parameters
            this.editedText = params.oneParameter;
        }
        return new ViewModel();
    }
    return {createViewModel: factory};
});

Related


Access Knockout component DOM from desired AMD viewModel

Silicon Valley I load my Knockout components this way: ko.components.register("example", { viewModel: {require: "widgets/example"}, template: {require: "text!widgets/example.html"} }); with example.js(extremely simplified): "use strict"; define(["kn

Access Knockout component DOM from desired AMD viewModel

Silicon Valley I load my Knockout components this way: ko.components.register("example", { viewModel: {require: "widgets/example"}, template: {require: "text!widgets/example.html"} }); with example.js(extremely simplified): "use strict"; define(["kn

Access ViewModel in Knockout Mapping Plugin

SapuA I'm using the knockout map plugin to add a computed property to an item in an observable array. However, this computed property depends on other properties in my view model. How can I access viewmodel properties when creating an observable during mapping

Access ViewModel in Knockout Mapping Plugin

SapuA I'm using the knockout map plugin to add a computed property to an item in an observable array. However, this computed property depends on other properties in my view model. How can I access viewmodel properties when creating an observable during mapping

Access Knockout ViewModel data in Sammy route

Brandon In my Knockout/Sammy SPA, I want to access ViewModel data in Sammy route, however, Sammy route is executed before my Knockout behavior . It works for the initial route because I set it self.selectedPage()equal to the first page, but not for subsequent

Access Knockout ViewModel data in Sammy route

Brandon In my Knockout/Sammy SPA, I want to access ViewModel data in Sammy route, however, Sammy route is executed before my Knockout behavior . It works for the initial route because I set it self.selectedPage()equal to the first page, but not for subsequent

Access 'this' from desired object literal?

Jack Wilson I would like to have objects of the following types: const Client = require('./Client') let client = new Client() client.someObject.create(...) client.someObject.update(...) client.someObject.delete(...) etc. It's easy to do something like this:

Access 'this' from desired object literal?

Jack Wilson I would like to have objects of the following types: const Client = require('./Client') let client = new Client() client.someObject.create(...) client.someObject.update(...) client.someObject.delete(...) etc. It's easy to do something like this:

Access 'this' from desired object literal?

Jack Wilson I would like to have objects of the following types: const Client = require('./Client') let client = new Client() client.someObject.create(...) client.someObject.update(...) client.someObject.delete(...) etc. It's easy to do something like this:

Access 'this' from desired object literal?

Jack Wilson I would like to have objects of the following types: const Client = require('./Client') let client = new Client() client.someObject.create(...) client.someObject.update(...) client.someObject.delete(...) etc. It's easy to do something like this:

Access 'this' from desired object literal?

Jack Wilson I would like to have objects of the following types: const Client = require('./Client') let client = new Client() client.someObject.create(...) client.someObject.update(...) client.someObject.delete(...) etc. It's easy to do something like this:

Access parent's dom node from child component react react

Alessandro I have a grid component like this: import React, { Component } from 'react'; import Action from './action.jsx'; class Grid extends Component { constructor(props) { super(props); this.maxN = 110; this.tempArray = [];

Access parent's dom node from child component react react

Alessandro I have a grid component like this: import React, { Component } from 'react'; import Action from './action.jsx'; class Grid extends Component { constructor(props) { super(props); this.maxN = 110; this.tempArray = [];

Access parent's dom node from child component react react

Alessandro I have a grid component like this: import React, { Component } from 'react'; import Action from './action.jsx'; class Grid extends Component { constructor(props) { super(props); this.maxN = 110; this.tempArray = [];