0

So I am using require.js and knockout.js together. I have a simple page that will the collect users.

Here is the code:

cshtml file:

@section scripts{
    <script data-main="../Scripts/app/usermanagement/app.invite" src="../Scripts/lib/require.js"></script>
}
<div id="usermanagement-invite-view">
    <p>Enter all the users you would like to invite to this account. You will be able to set the permissions onces you have added them.</p>
    <span data-bind="text: ko.toJSON($data)"></span>
    <div data-bind="foreach: invitedUsers()">
        <div><span data-bind="text: firstName"></span></div>
        <div><span data-bind="text: lastName"></span></div>
        <div><span data-bind="text: emailAddress"></span></div>
    </div>
    <div>
        <input type="text" placeholder="First Name" data-bind="text: firstName" class="input" />
        <input type="text" placeholder="Last Name" data-bind="text: lastName" class="input" />
        <input type="email" placeholder="EmailAddress" data-bind="text: emailAddress" class="input" />
        <input type="button" value="Invite" data-bind="click: invite" class="submit" />
    </div>
</div>

Here is the app.invite.js

requirejs.config({
    "baseUrl": "../Scripts/app/usermanagement",
    "paths": {
        "app.invite": "app.invite",
        "ko": "../../lib/knockout-2.2.1",
        "jquery": "//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min",
        "toastr": "../../lib/toastr"
    }
});

// Load the main app module to start the app
require(["main.invite"], function (bs) { bs.run(); });

Here is the main.invite.js:

define(['ko', 'inviteViewModel'], function (ko, inviteViewModel) {
    var
        run = function () {
            var vm = new inviteViewModel();
            ko.applyBindings(vm, document.getElementById('#usermanagement-invite-view'));
        };
    return {
        run: run
    };
});

Here is the inviteViewModel:

define('inviteViewModel', ['jquery', 'ko', 'toastr', 'model.user'], function ($, ko, toastr, User) {

    return function inviteViewModel() {
        var self = this;

        self.firstName = ko.observable();
        self.lastName = ko.observable();
        self.emailAddress = ko.observable();

        self.invitedUsers = ko.observableArray();

        self.invite = function () {
            var newUser = new User();
            newUser.firstName(self.firstName());
            newUser.lastName(self.lastName());
            newUser.emailAddress(self.emailAddress());

            self.invitedUsers().push(newUser);
            console.log(self.invitedUsers());


        };

    }
});

For some reason, when i call the invite function, the code shows that I am adding something to collection, but the UI doesn't get updated. Here what it looks like when i do a console.log. This however doesn't show the value being

Console Log

What am I doing wrong?

1 Answer 1

2

You need to call push directly on the observableArray in order to KO tracks the changes for you and updates the UI, so you don't need the parenthesis:

self.invitedUsers.push(newUser);

See also in the documentation:

pop, push, shift, unshift, reverse, sort, splice

All of these functions are equivalent to running the native JavaScript array functions on the underlying array, and then notifying listeners about the change:

myObservableArray.push('Some new value') adds a new item to the end of array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.