How to use Knockout.Mapping with a viewmodel with behaviour

3,850 views
Skip to first unread message

Chris

unread,
May 10, 2011, 4:28:47 PM5/10/11
to KnockoutJS
Hi,

I have a view model that has properties and behaviors and I want to
use mapping plugin to set the properties from the server. I can get it
working correctly without behaviors, but when I try to declare the
object first (like below) I cant get access to the function... I
assumed the dayModel is being overridden by the mapping, and tried the
update method instead... no joy.



var dayModel = {
DayName: ko.observable(),

hithere: function () {
alert('Hi');
}
}

$.getJSON("@Url.Action("GetDay")", function(data) {
dayModel = ko.mapping.updateFromJS(dayModel, data);
ko.applyBindings(dayModel);

});


any help much appreciated, and if this is a newbie question I
apologise!!

Mark Bradley

unread,
May 10, 2011, 9:15:14 PM5/10/11
to knock...@googlegroups.com
from here:
http://knockoutjs.com/documentation/plugins-mapping.html#example_using_komapping

You don't assign the viewModel to the result of ko.mapping.updateFromJS.

$.getJSON("@Url.Action("GetDay")", function(data) {

ko.mapping.updateFromJS(dayModel, data);
// ko.applyBindings(dayModel); // this probably isn't needed either.
});

--
-barkmadley
sent from an internet enabled device

Chris

unread,
May 11, 2011, 4:48:56 AM5/11/11
to KnockoutJS
I modified as below, but I now get "The object you are trying to
update was not created by a 'fromJS' or 'fromJSON' mapping."

I can get it working fro properties updates if I follow the sample
code on the site, but how do I then attached methods to the object?
i.e my HiThere function.


var dayModel = {
DayName: ko.observable(),

hithere: function (e) {
alert(e);
}

}


$(function () {
$.getJSON("@Url.Action("GetDay")", function(data) {
ko.mapping.updateFromJS(dayModel, data);
});

ko.applyBindings(dayModel);
});

Roy Jacobs

unread,
May 11, 2011, 5:13:41 AM5/11/11
to knock...@googlegroups.com
Can you try this:

var dayModelData = { 
   DayName: "initialValue"
};

var dayModel = ko.mapping.fromJS(dayModelData);
dayModel.hithere = function() { alert('Hi'); }

Basically, this will take an existing javascript object ('dayModelData') and map it to an observable object. Then, once you have this object, you can add your behaviors. Finally, you can use updateFromJS like described by Mark Bradley.

Hope this helps,
Roy

Robert Byrne

unread,
May 11, 2011, 5:18:15 AM5/11/11
to knock...@googlegroups.com
Hi Chris,

You have to call ko.mapping.fromJS at least once on the object. The trick is to pass in your view model as the third argument (the second argument is custom mapping options, which you can pass in as null)

Doing is this way preserves the original view model instance, which keeps your default fields and functions around. See below.

Rob.

    var dayModel = { 
        DayName: ko.observable(), 

        hithere: function (e) { 
        alert(e); 
        } 

    }
    
     $(function () { 
        $.getJSON("@Url.Action("GetDay")", function(data) { 
            ko.mapping.fromJS(data, null, dayModel); 
        }); 

        ko.applyBindings(dayModel); 
    });

Chris

unread,
May 11, 2011, 5:27:23 AM5/11/11
to KnockoutJS
Thanks Robert! Works a treat.... :)

On May 11, 10:18 am, Robert Byrne <kapi...@gmail.com> wrote:
> Hi Chris,
>
> You have to call ko.mapping.fromJS at least once on the object. The trick is
> to pass in your view model as the third argument (the second argument is
> custom mapping options, which you can pass in as null)
>
> Doing is this way preserves the original view model instance, which keeps
> your default fields and functions around. See below.
>
> Rob.
>
>     var dayModel = {
>         DayName: ko.observable(),
>
>         hithere: function (e) {
>         alert(e);
>         }
>
>     }
>
>      $(function () {
>         $.getJSON("@Url.Action("GetDay")", function(data) {
>             ko.mapping.fromJS(data, null, *dayModel*);
>         });
>
>         ko.applyBindings(dayModel);
>     });
Reply all
Reply to author
Forward
0 new messages