Sateesh Pagolu
Sateesh Pagolu

Reputation: 9606

knockout JS: Unable to See Expected Results

I am trying to learn knockout by writing a small application. My code is very simple. I might be doing a small mistake, so I am not getting any results displayed.

Below is the code

Index.cshtml

    <div>
    <ul data-bind="foreach:list">
        <li>
            <span data-bind="text:name"></span>
        </li>
    </ul>
</div>

<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="~/Scripts/knockout-2.1.0.js" type="text/javascript"></script>
<script src="~/Content/MyScripts/PracticeScript.js" type="text/javascript"></script>

PracticeScript.js

    $(function () {
    var data = [
    { name: "microsoft" },
    { name: "google" },
    { name: "facebook" },
    { name: "twitter" },
    { name: "Apple" }
    ];

    var viewModel = {
        item: ko.observable(""),
        list: ko.observableArray(data),
        addNewItem: function () {
           this.list.push(this.item());
        }
    };
    ko.applyBindings(this.viewModel);
});

Upvotes: 0

Views: 55

Answers (2)

Jacques Snyman
Jacques Snyman

Reputation: 4290

Remove the this from your ko.applyBindings

ko.applyBindings(viewModel);

Fiddle here

Upvotes: 1

ebram khalil
ebram khalil

Reputation: 8321

In your example you pass this.viewModel, but viewModel object does not belong to this..

so basicly you are passing undefined as your viewModel instead of the actual object.

DEMO

Upvotes: 1

Related Questions