Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shouldn't mapping an array property apply options to each element rather than the entire array? #188

Open
togakangaroo opened this issue Feb 3, 2014 · 1 comment

Comments

@togakangaroo
Copy link

Maybe I'm not getting something but say I have this json:

{
  id: 1
  firstName: "Greg"
  toys: [
     {id: 2, name: "Mr Teddypants"},
     ... 
  ]
}

And I want to map everything to observables as default except for id properties. I would think that the following custom would work

{
  copy: ['id']
  toys: { copy: ['id'] }
}

but it doesn't because the custom toys mapping seems to apply to the entire array, not each item. The only way I've found of applying this to the child items is to tear out how the object is constructed entirely

{
  copy: ['id']
  toys: { 
    create: function(op){ 
      return ko.observableArray(op.data.map(function(t){ 
        return mapping.fromJS( t, {copy: ['id'] })
      }))
}

This is pretty anti-intuitive and is almost more code than mapping things manually! In fact, if there's properties for schoolSupplies, friends, bullies, now I'm writing layers of abstraction just to generate the mapping.

Am I missing something? If not, then I think it makes a lot more sense for properties like copy or ignore on arrays to apply to the elements rather than the array itself.

@ropez
Copy link

ropez commented Aug 29, 2014

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants