Code Monkey home page Code Monkey logo

Comments (11)

grofit avatar grofit commented on September 27, 2024

is it appending? as I am sure there is a boolean you can pass in that tells it to replace or append.

from knockout.merge.

grofit avatar grofit commented on September 27, 2024

Turns out I was thinking of the merge constructor:

ko.observableArray.fn.withMergeConstructor = function(mergeConstructor, replaceOnMerge) {
        this.mergeConstructor = mergeConstructor;

        if(replaceOnMerge) { 
            this.replaceOnMerge = replaceOnMerge; 
        }

        return this;
    }

Which has the boolean to replace or append on merge, so wont be applicable here.

from knockout.merge.

grofit avatar grofit commented on September 27, 2024

On a side note given a few people use this lib now it may be worth adding some unit tests to it so at least there are some scenarios to test for all these things, if I get a moment in the week I will try to add some.

from knockout.merge.

IPWright83 avatar IPWright83 commented on September 27, 2024

@grofit some automated tests would be good. I get a little lost as soon as people start getting complex build processes, but happy to try and add some simple Jasmine tests etc at some point. I'm basically writing a couple of small tests in a JSFiddle while I'm trying to fix.

from knockout.merge.

grofit avatar grofit commented on September 27, 2024

Yeah I didnt want to require nodejs to test it all, and I just never bothered adding jasmine. Have done so now so will add more tests as I go, feel free to fill in the gaps.

from knockout.merge.

IPWright83 avatar IPWright83 commented on September 27, 2024

@grofit almost got a fix for this. I'll add some tests when I create a fork. Quick question - you ever had any issues with observable arrays not updating subscribers?

I'm currently using the following:

// If we have an observable array and a data element which is an array
// then we need to merge the values item by item
for (var i = 0; i < dataElement.length; i++) {

    // We don't yet have an item in the array so we need to 
    // create one. Use a placeholder and the standard merge
    // logic to do this
    if (i >= knockoutElement().length) {
        var placeholder = {};
        exports.fromJS(placeholder, dataElement[i]);
        knockoutElement.push(placeholder);
    } else if (isPrimitive(knockoutElement()[i]) && isPrimitive(dataElement[i])) {
        knockoutElement().splice(i, 1, dataElement[i]);
    } else {
        exports.fromJS(knockoutElement()[i], dataElement[i]);
    }
}

What I'm finding is that I jump to the knockoutElement().splice(i, 1, dataElement[i]); and the array gets updated correctly, but anything that's listening to the array doesn't seem to update properly. I've had to put a knockoutElement.notifySubscribers(); to get this working properly in our codebase but I can't quite figure out why I need it.

from knockout.merge.

grofit avatar grofit commented on September 27, 2024

Not that I can remember, what version of KO are you using?

from knockout.merge.

IPWright83 avatar IPWright83 commented on September 27, 2024

@grofit 3.3 debug version from cdnjs.com

from knockout.merge.

IPWright83 avatar IPWright83 commented on September 27, 2024

@grofit nevermind, had an extra () that I didn't need in there. Got a fix, will write some tests quick then issue a pull request. Thanks :)

from knockout.merge.

IPWright83 avatar IPWright83 commented on September 27, 2024

@grofit love the speed at which I submit a pull request and it gets merged in :) Thanks!

from knockout.merge.

grofit avatar grofit commented on September 27, 2024

❤️ Github

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.