Knockout.js component parameter undefined


Arnold Wiersma

I am creating a Knockout js component that can be used in a notification system. When I add a component to the page I get a params is undefined message, it looks like my params object is not sent to the viewmodel's constructor.

At first I thought my component loader was the problem, so I manually registered the component like this:

ko.components.register('notification-info', {
    viewModel: { require: "/notifications/info" },
    template: { require: "text!/notifications/info.tmpl.html"}
});

The component consists of a js file called info.js

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

function InfoNotificationViewModel(params) {
    this.type = params.type;
    this.subject = params.subject;
    this.title = params.title;
    this.content = params.content;
}

return InfoNotificationViewModel();
});

and an html template called info.tmpl.html

<div class="notification-container" data-bind="css: type">
<div class="notification-icon"><span class="glyphicon"></span></div>
<div class="notification-content">
    <div class="notification-subject" data-bind="text: subject">
    </div>
    <div class="notification-message">
        <p data-bind="text: title"></p>
        <p data-bind="text: content"></p>
    </div>
</div>

I add the component to the page using component binding:

<div data-bind="component: {name: 'notification-info', params: {type: 'info', subject: '',title: '', content: ''} }"></div>

Later, the data sent over the websocket will be populated so that the parameters can be observed.

Can someone tell me what I'm doing wrong, I think it has something to do with the way I'm registering the component.

Domi Lake

You should return the constructor of the viewmodel, so don't

return InfoNotificationViewModel();

use

return InfoNotificationViewModel;(without parentheses)

Related


Undefined parameter in react component

Sergio I have a navbar component with a simulated login that brings me the user email from the modal, but when I send a property to my signinmenu component, the email is showing as undefined. I also sent a logout function which works but the string email shows

Undefined parameter in react component

Sergio I have a navbar component with a simulated login that brings me the user email from the modal, but when I send a property to my signinmenu component, the email is showing as undefined. I also sent a logout function which works but the string email shows

Knockout JS foreach $root undefined

quick shift I am trying to create a radio group in Knockout JS, here is the template code <p>Selected Plan <div data-bind="text: selectedPlan"></div></p> <div data-bind="foreach: plans"> <label> <input type="radio" name="plan" data-bind="attr: {val

Knockout JS foreach $root undefined

quick shift I am trying to create a radio group in Knockout JS, here is the template code <p>Selected Plan <div data-bind="text: selectedPlan"></div></p> <div data-bind="foreach: plans"> <label> <input type="radio" name="plan" data-bind="attr: {val

validateObservable in Knockout JS - undefined is not a function

Ian Reunion, I'm trying to validate a very large form and basically rewrote all of my viewModel code so that it can be validated. The problem is that ko.validatedObservable() doesn't seem to exist for some reason. I've used the code from this site but honestly

Component Communication in Knockout.JS

Mathias Mamsch I'm designing a "medium" sized app in KnockoutJS and I'd like to know how to send events between components. Imagine a nested component hierarchy in KnockoutJS: Root Viewmodel -> A -> B -> C -> D How does D respond to the message

Component Communication in Knockout.JS

Mathias Mamsch I'm designing a "medium" sized app in KnockoutJS and I'd like to know how to send events between components. Imagine a nested component hierarchy in KnockoutJS: Root Viewmodel -> A -> B -> C -> D How does D respond to the message

Knockout js pass view parameter

safe I have a parameter in my mvc view as @{ var myparam = false; } My button is the same as the following view: <input type="button" id="myButton" value="Click" class="btn btn-primary" data-bind='enable: selectvalue() != ""' /> In my button's data bi

Knockout using $data as component parameter in $foreach

marion I'm starting with components, but I'm trying to use this simple code. When I put a breakpoint in the function viewModel(params), the object params.datais my viewmodel viewModelHelloWorldand I want it to be an item of my array. What am I doing wrong? HTM

Knockout using $data as component parameter in $foreach

marion I'm starting with components, but I'm trying to use this simple code. When I put a breakpoint in the function viewModel(params), the object params.datais my viewmodel viewModelHelloWorldand I want it to be an item of my array. What am I doing wrong? HTM

Knockout using $data as component parameter in $foreach

marion I'm starting with components, but I'm trying to use this simple code. When I put a breakpoint in the function viewModel(params), the object params.datais my viewmodel viewModelHelloWorldand I want it to be an item of my array. What am I doing wrong? HTM

Knockout using $data as component parameter in $foreach

marion I'm starting with components, but I'm trying to use this simple code. When I put a breakpoint in the function viewModel(params), the object params.datais my viewmodel viewModelHelloWorldand I want it to be an item of my array. What am I doing wrong? HTM

Knockout using $data as component parameter in $foreach

marion I'm starting with components, but I'm trying to use this simple code. When I put a breakpoint in the function viewModel(params), the object params.datais my viewmodel viewModelHelloWorldand I want it to be an item of my array. What am I doing wrong? HTM

Knockout using $data as component parameter in $foreach

marion I'm starting with components, but I'm trying to use this simple code. When I put a breakpoint in the function viewModel(params), the object params.datais my viewmodel viewModelHelloWorldand I want it to be an item of my array. What am I doing wrong? HTM

Knockout using $data as component parameter in $foreach

marion I'm starting with components, but I'm trying to use this simple code. When I put a breakpoint in the function viewModel(params), the object params.datais my viewmodel viewModelHelloWorldand I want it to be an item of my array. What am I doing wrong? HTM

Knockout using $data as component parameter in $foreach

marion I'm starting with components, but I'm trying to use this simple code. When I put a breakpoint in the function viewModel(params), the object params.datais my viewmodel viewModelHelloWorldand I want it to be an item of my array. What am I doing wrong? HTM

Undefined knockout? Does it require js to load anything?

The internet So consider the following: addExternalLibrary: function(name, url) { _externalLibraries[name] = url; _librariesToLoad.push(name); }, setUpRequireJS: function() { define('jquery', [], function() { return jQuery; }); requirejs.co

ObservableArray throws undefined is not a function in Knockout.js

User 373455 I have a problem with the following code crashing self.DivisionRuleList.push(div); I get the following error: Uncaught TypeError: undefined is not a function The list should be initialized because I'm using the same list in another dropdown (rend

Knockout.js: Local Observables in Component ViewModels

Steve Johnstone I'm using a knockout component (loaded via require.js) to create a login widget. Javascript: ko.components.register('login-widget', { viewModel: { require: '/components/login-widget.js' }, template: { require: 'text!/components/login-wi

Knockout JS: Pass observable array as parameter

trunk I have a 2d observable array called textFields: var t1 = ko.observableArray([{val: 0}, {val:0}]) self.textFields = ko.observableArray([t1]); I have a function called Solve that should take one parameter: self.solve = function(arr){ console.log(arr); }

Angular component parent function undefined parameter

Coty I have a component that binds a parent function. I know I have to use non-primitive values as parameters, but I'm still not sure. How is this going? Here is sample code demonstrating my problem. Components: app.component('testComponent', { template:'<bu

Undefined component parameter in angular2

Vladimir Tried to figure this out, but couldn't... Maybe it's obvious: Call the "todo" component in HTML: <div *ngFor="let item of todoLists" class="row"> <div class="col-xl-7 col-lg-7 col-md-12 col-sm-12 col-xs-12 bottom-15"> <todo [listName]="i

Undefined component parameter in angular2

Vladimir Tried to figure this out, but couldn't... Maybe it's obvious: Call the "todo" component in HTML: <div *ngFor="let item of todoLists" class="row"> <div class="col-xl-7 col-lg-7 col-md-12 col-sm-12 col-xs-12 bottom-15"> <todo [listName]="i

Undefined component parameter in angular2

Vladimir Tried to figure this out, but couldn't... Maybe it's obvious: Call the "todo" component in HTML: <div *ngFor="let item of todoLists" class="row"> <div class="col-xl-7 col-lg-7 col-md-12 col-sm-12 col-xs-12 bottom-15"> <todo [listName]="i

Angular component parent function undefined parameter

Coty I have a component that binds a parent function. I know I have to use non-primitive values as parameters, but I'm still not sure. How is this going? Here is sample code demonstrating my problem. Components: app.component('testComponent', { template:'<bu

Undefined component parameter in angular2

Vladimir Tried to figure this out, but couldn't... Maybe it's obvious: Call the "todo" component in HTML: <div *ngFor="let item of todoLists" class="row"> <div class="col-xl-7 col-lg-7 col-md-12 col-sm-12 col-xs-12 bottom-15"> <todo [listName]="i