0

Below is my view model code

var TopicsViewModel = function() {
var self = this;

var fakeTopicData = [];


self.Topic = function(area, name, link, desc, why) {
    self.area = ko.observable(area ? area : "");
    self.name = ko.observable(name ? name : "");
    self.link = ko.observable(link ? link : "");
    self.desc = ko.observable(desc ? desc : "");
    self.why = ko.observable(why ? why : "");
};


self.createProfile = function () {
    alert("came to create profile");
};

self.editProfile = function () {
    alert("came to edit profile");
};

self.removeProfile = function (profile) {
    alert("came to remove profile");

    self.topicsArr.remove(profile);
};



var refresh = function () {
    fakeTopicData.push(new self.Topic("Functional Prog", "Javascript", "http:\\www.somedummysite.com", "somedesc", "Just because"));
    fakeTopicData.push(new Topic("ASP.NET ", "MVC5", "http:\\www.somedummysite.com", "somedesc2", "Just because2"));


     self.topicsArr(fakeTopicData);
};


self.topicsArr = ko.observableArray([]);
refresh();

  };
  ko.applyBindings(new TopicsViewModel());

Here is my VIEW code

<hr /> 
<hr /> 
@*<input type="button"  class="btn-sm"  value="New Topic" data-bind="click:clickevent" />*@
<table  class="table table-striped table-bordered table-condensed">
    <tr >
        <th>Area</th>
        <th>Name</th>
        <th>Link</th>
         <th>Link</th>
        <th>Description</th>
        <th>Why</th>

    </tr>
    <tbody data-bind="foreach : topicsArr">   
        <tr>
            <td data-bind="text:area"> </td>
            <td class=""><a data-bind="text:name, click:$parent.editProfile"></a></td>                
            <td data-bind="text:link"> </td>
            <td> <input data-bind="text:link"></> </td>
            <td data-bind="text:desc">  </td>
            <td data-bind="text:why" ></td>
            <td><button class="btn btn-mini btn-danger" data-bind="click:$parent.removeProfile">remove</button></td>
        </tr>


    </tbody>
</table>

@* *@

I was hoping that when the user makes a change to the input field the value will be reflected in the model object. But when i run this app I get below error

0x800a139e - JavaScript runtime error: Unable to parse bindings.

Message: ReferenceError: 'area' is undefined;

Could someone please point me to what I am doing wrong here.

2 Answers 2

1

To get your code work, you can add if condition to your HTML. Experts may point out the exact mistake Rekha :).

<tbody data-bind="foreach: topicsArr">
    <!-- ko if: $parent.topicsArr != 'undefined'  -->
    <!-- ko if: $parent.topicsArr.length > 0  -->
    <tr>
        <td data-bind="text: area"></td>
        <td class=""><a data-bind="text: name, click: $parent.editProfile"></a></td>
        <td data-bind="text: link"></td>
        <td>
            <input data-bind="text: link"></> </td>
        <td data-bind="text: desc"></td>
        <td data-bind="text: why"></td>
        <td>
            <button class="btn btn-mini btn-danger" data-bind="click: $parent.removeProfile">remove</button></td>
    </tr>
    <!-- /ko -->
    <!-- /ko -->
</tbody>

Okay, when you look at you TopicsViewModel, you'll see something like below:- enter image description here

So you need $parent to access your elements.

 <tr>
            <td data-bind="text: $parent.area"></td>
            <td class=""><a data-bind="text: name, click: $parent.editProfile"></a></td>
            <td data-bind="text: $parent.link"></td>
            <td>
                <input data-bind="text: $parent.link"></> </td>
            <td data-bind="text: $parent.desc"></td>
            <td data-bind="text: $parent.why"></td>
            <td>
                <button class="btn btn-mini btn-danger" data-bind="click: $parent.removeProfile">remove</button></td>
        </tr>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much. Adding the $parent got me past the error. <td data-bind="text: $parent.link"></td> <td><input data-bind="text: $parent.link"></> </td> I had expected the text value to get updated when I updated the input value as they are bound to the same observable field. This is not happening. On the view I can type in a new value for link and the text value <td> element is not updated
However I had expected the
0

I had to have the input data element binding as data-bind="VALUE: .... if the data-bind="text: it will not update the value of the observable . Hopefully this will help someone else who runs into the same issue.

  <td data-bind="text: $parent.link"></td>
  <td><input data-bind="value: $parent.link"></> </td>

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.