Updating or inserting an item in an observable array #41

Closed
luv2code opened this Issue Dec 9, 2011 · 7 comments

Comments

Projects
None yet
2 participants

luv2code commented Dec 9, 2011

this situation is that objects are pumped from a service one at a time and stored in an array. Also, updates to those objects are received one at a time. When there is a key defined for the mappedObservableArray, it would be nice if fromJS inserted or updated based on key match.

for instance:

   var vm =ko.mapping.fromJS( [ ko.mapping.formJS({name:'matt', age:20}) ], {
        key:function(data) {
            return ko.utils.unwrapObservable(data.name);
        }
    });

    ko.mapping.fromJS(ko.mapping.fromJS({ name: 'jim', age: 23 }), vm); //should insdert jim into the array

    ko.mapping.fromJS(ko.mapping.fromJS({ name: 'matt', age: 23 }), vm); //should change matt's age to 23

I use this pattern quite a lot.
currently, I'm doing this:

    var index = vm.mappedIndexof(data);
    if(index < 0) { 
         vm.push(ko.maping.fromJS(data));
    } else {
        ko.mapping.fromJS(data, vm()[index]);
    }
Collaborator

RoyJacobs commented Dec 9, 2011

The reason the mapping plugin does not do this, is that there is no clean way to signal deletions.

luv2code commented Dec 9, 2011

I think it would be alright to not be expected to support deletions this way. I mean, there really isn't a clean way to do it. So, one might be expected to explicitly call a mappedRemove(key).

but I can dig it if you don't want to add it to the library.

Collaborator

RoyJacobs commented Dec 9, 2011

No, I think this alternate behavior could also be a nice feature. But it
would have to be explicitly enabled using a mapping configuration option.
This way it wouldn't break any existing code.
If you can add a pull request implementing that, it would be very welcome :)

On Fri, Dec 9, 2011 at 17:40, Matthew Taylor <
reply@reply.github.com

wrote:

I think it would be alright to not be expected to support deletions this
way. I mean, there really isn't a clean way to do it. So, one might be
expected to explicitly call a mappedRemove(key).

but I can dig it if you don't want to add it to the library.


Reply to this email directly or view it on GitHub:

#41 (comment)

luv2code commented Dec 9, 2011

turn it on like this?

var vm = ko.mapping.fromJS([], { arrayUpdate: true, arrayInsert:true });
Collaborator

RoyJacobs commented Dec 9, 2011

well, more something along the lines of { incrementalArrayUpdates: true }
to indicate that what you receive from the server is an incremental update,
instead of a 'full' state.
additionally, if the data from the server contains a "_destroy" property in
an array, that could be an indication to delete (similar to what you can do
with array.destroy(item) in an observable array).

On Fri, Dec 9, 2011 at 19:13, Matthew Taylor <
reply@reply.github.com

wrote:

turn it on like this?

var vm = ko.mapping.fromJS([], { arrayUpdate: true, arrayInsert:true });

Reply to this email directly or view it on GitHub:

#41 (comment)

I gave this a shot; but I'm out of time. the updateViewModel function is hard to get my mind around. I kept breaking other unit tests without actually getting my change to work. I strongly feel that the function is too large and needs to be refactored into smaller chunks.

I'm giving up for now. I might come back to it later.

Collaborator

RoyJacobs commented Dec 14, 2011

Fair enough, development of the plugin has been fairly, shall we say, iterative :) So there is definitely a lot to be gained by cleaning it up. Sadly, with limited time on my side this has fallen by the wayside.

By the way, if you have any suggestions on restructuring the code and so on, by all means let me know. Good suggestions are always welcome!

RoyJacobs closed this Feb 6, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment