This is a migrated thread and some comments may be shown as answers.

Problem using get and set on data in an array

2 Answers 47 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Garrett
Top achievements
Rank 2
Garrett asked on 28 Jun 2012, 03:08 PM
I have a view-model with a property called fields that is attached to my datasource's view, which will have some data similar to this:

[{
    Id: 1,
    Name: 'Email',
    Type: 'Email',
    Default: '',
    Display: true,
    Identifier: true
}, {
    Id: 2,
    Name: 'First name',
    Type: 'Text',
    Default: 'Fistname',
    Display: true,
    Identifier: false
}, {
    Id: 3,
    Name: 'Last name',
    Type: 'Text',
    Default: 'Lastname',
    Display: true,
    Identifier: false
}, {
    Id: 4,
    Name: 'City',
    Type: 'Text',
    Default: '',
    Display: true,
    Identifier: false
}]

I'm using the following row-template to make a sort of grid with a column that has an edit button in it. 

<script id="row-template" type="text/x-kendo-template">
    <tr data-uid="#= Id #">
        <td align="center">
            <a href="javascript:void(0);" data-bind="click: edit" class="editRow">edit</a>
        </td>
        <td data-bind="text: Name"></td>
        <td data-bind="text: Type"></td>
        <td data-bind="text: Default"></td>
        <td align="center"><input type="checkbox" data-bind="checked: Display" /></td>
        <td align="center"><input type="checkbox" data-bind="checked: Identifier" /></td>
    </tr>
</script>

This all works great until the click event (edit function in view-model) is called. While I am inside the edit function I can not use the get or set functions to work with the data unless I state the index of the entry from the view-model I want to edit. 

edit: function(e){
    this.set('fields[' + (e.data.Id -1) +'].Default', 'Clicked');
}

This will only works as long as the Ids in my data start at 1 and are always sequential, which is not very likely. You can see this here (http://jsfiddle.net/garrett55/aYMRn/) in the fiddle I created for this post.

Am I missing the right way to do this? Is there an easier way to get the index of the data I want in the view-model?

2 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 29 Jun 2012, 02:12 PM
Hi Garrett M,

Instead of getting the instance of the model through the fields array you may directly modify the model instance passed to the click handler:

edit: function(e){
    e.data.set("Default", "Clicked");       
}


Regards,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Garrett
Top achievements
Rank 2
answered on 29 Jun 2012, 02:21 PM
You guys rock, thank you!
Tags
MVVM
Asked by
Garrett
Top achievements
Rank 2
Answers by
Rosen
Telerik team
Garrett
Top achievements
Rank 2
Share this question
or