ko.mapping.fromJS 3rd parameter (the target) missing in new version

1,434 views
Skip to first unread message

Nelson

unread,
Jan 13, 2012, 4:46:13 PM1/13/12
to KnockoutJS
Hi, why there isn´t the 3rd parameter anymore in the ko.mapping.fromJS
function? (or it´s just me not finding it?)

I user to have some like like this:

function MyClass(obj){ //obj from json decode
this.fields = ko.observable('default values);
.
.
.
if(obj)
ko.mapping.fromJS(obj, { /* my rules(create etc) */ },
this);
}

var a = new MyClass({some: 'thing'});


And it used to worked very well... replacing the defaults by the "obj"
fields. When I upgraded the knockoutjs and the mapping plugin, it just
doesn´t work...

The workaround I found was:

$.extend(self, ko.mapping.fromJS(data, {}));

but why?!

JohnEarles

unread,
Jan 14, 2012, 8:45:34 AM1/14/12
to KnockoutJS
It seems to be still working for me.

Does this fiddle show what you expect: http://jsfiddle.net/WHNFf/

Robert Bell

unread,
Jan 24, 2012, 10:33:53 PM1/24/12
to KnockoutJS
What are the parameters?

BTW - awesome to finally find an example of fromJS that works for an
update. I've been looking all night on here and only see examples of
people showing how it does not work.

JohnEarles

unread,
Jan 25, 2012, 6:23:06 AM1/25/12
to KnockoutJS
Hi Robert,

In the call "ko.mapping.fromJS({attr2: 'two'}, { }, self)" the
parameters are:

data - The JavaScript object you have obtained and wish to map
options - An object literal describing any custom mapping options you
want applied (create, update, key, include, ignore, copy)
target - The JavaScript object that will be the target of the mapping

The documentation can explain the mapping options better than I can:
http://knockoutjs.com/documentation/plugins-mapping.html

Robert Bell

unread,
Jan 25, 2012, 7:15:19 AM1/25/12
to KnockoutJS
Thank you.

I'm stuck at trying to get this to work with an array of arrays. Does
fromJS only go one array level deep?

...
var TDrow = [
{ "Column": [
{ "ColumnType": "Question", "id": "5",
"displayText": "First Name", "canDelete": true, "Group": "",
"hasRule": true, "visible": true }
]
}];
....
self.TDrows = ko.mapping.fromJS(TDrow );
....
<ul data-bind="template: {name:'twoDtemplate', foreach: vm.TDrows}">
...
<script id="twoDtemplate" type="text/html">
<li data-bind="text: Column[0].displayText" ></li>
</script>
...
Error: Cannot read property 'displayText' of undefined;

JohnEarles

unread,
Jan 25, 2012, 7:24:57 AM1/25/12
to KnockoutJS
By default, yes. You can however write a custom create option that
will call mapping again on itself.

See this question for an example:

http://groups.google.com/group/knockoutjs/browse_thread/thread/2365f928ec0692ca

JohnEarles

unread,
Jan 25, 2012, 7:37:09 AM1/25/12
to KnockoutJS
Can you create a JSFiddle @ http://jsfiddle.net/? It would be easier
to try and figure out your issue with a fiddle that shows the issue.
You don't need to signup for an account, if you don't want to... you
can create an anonymous fiddle.

Robert Bell

unread,
Jan 25, 2012, 2:01:04 PM1/25/12
to KnockoutJS
I tried to reply to you but it must have not taken. Thank you so much
for the thread you pointed me to. That did the trick. My wife would
be so much happier with me if I had found you two days earlier, but
I'll be home for dinner tonight. Again, thank you.

On Jan 25, 7:37 am, JohnEarles <jear...@gmail.com> wrote:
> Can you create a JSFiddle @http://jsfiddle.net/? It would be easier
Reply all
Reply to author
Forward
0 new messages