Code Monkey home page Code Monkey logo

Comments (5)

grofit avatar grofit commented on September 21, 2024

I dont think there is out of the box, but feel free to add that functionality if you want, as long as the tests pass you should be fine to add features.

from knockout.merge.

deblocker avatar deblocker commented on September 21, 2024

THX for your answer,
i'm asking because i'm in doubt if the line :
exports.fromJS(arrayElement, element, options);
will traverse recursively the viewmodel?
please correct me if i'm wrong, the test shows only one level deep - do you think my change could be an issue with nested observableArrays on many levels?

from knockout.merge.

grofit avatar grofit commented on September 21, 2024

to my knowledge it traverses indefinitely (which could lead to it hanging with circular references), so the above code should automatically populate the array with whatever is in the other array. The only issue I can see with your above request is that there are 2 scenarios with arrays:

  • Appending to an array
  • Replacing array

So in the scenario where you want to append to an array (I think thats what the last arg is for on the mergeConstructor bit) you dont want to replace the whole array content and just push into it regardless of if its one or twenty new ones. In this scenario if performance is paramount you could always copy the internal array out, append to that then bulk add it back in, I am not sure how that would effect knockout if there were complex objects in the array etc.

In the second case you can just replace the array in bulk, as that would basically just overwrite all elements anyway.

from knockout.merge.

deblocker avatar deblocker commented on September 21, 2024

as usual in life, in the viewmodel i'm working on, there is a mix of both: appending and replacing.

Consider the metaphor of a truck with trailers: new trailers are appended to an observablearray with some data passed to the constructor, but each trailer must have a fixed number of rails, so these new rails are also always created as a fixed number children objects of a trailer, let say 4, because they have a positional key and shall be not interchangeable: front-left, front -right, and so on. So, these rails should be mapped as an array, because they gets never added or removed. But children properties of the rails are observables, as rails are complex objects, and shall be updated through their "position" key, like the ko.mapping plugin does.
So, my scenario is quite complex but i think could be also very common in real-life applications.

Before rolling out my own custom solution, i will play with the mergeRules to see if i have some more luck...

Thank you for your time grofit, i appreciated your help, really!

from knockout.merge.

grofit avatar grofit commented on September 21, 2024

It has been a long time since I have been doing stuff with this library as it works for most simple scenarios without much effort, this being said I do remember one of the last changes I made opened it up for adding custom handlers with custom options (Global handlers I think).

So it may be possible (although it will cause a bit of overhead) to just write a custom global handler which would check to see if your data is an array, and if it falls into the category of being a replacement or an appending and then just do the relevant operation.

Although if performance is a concern it may be worth you forking and adding the functionality in there so you don't get too much overhead on scanning.

Anyway if you have any queries as you go feel free to continue the conversation and I can try to assist.

from knockout.merge.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.