4imble
4imble

Reputation: 14416

KnockoutJs 1.3 beta. _destroy:false has same result on ui as _destroy:true

Using asp.net mvc, I am passing a viewmodel down and getting knockout to map a viewmodel and bind to this.

This is all working fine for me, what i am trying to do is track deletions.

I thought i would be able to do this by adding the _destroy property but setting it to false.

I hoped that the ui would then ignore this until destroy set it to true.

But this does not seem to be the case and the mere presence of this property is causing to be treated as destroyed.

Is this a bug or am I handling this wrong?

Many thanks, Kohan

  var model = [{"Id":1,"Name":"Bikes","Parent":null,"_destroy":false},
  {"Id":2,"Name":"Components","Parent":null,"_destroy":false},
  {"Id":3,"Name":"Clothing","Parent":null,"_destroy":false},
  {"Id":4,"Name":"Accessories","Parent":null,"_destroy":false},
  {"Id":5,"Name":"Mountain Bikes","Parent":1,"_destroy":false},
  {"Id":6,"Name":"Road Bikes","Parent":1,"_destroy":false},
  {"Id":7,"Name":"Touring Bikes","Parent":1,"_destroy":false},
  {"Id":8,"Name":"Handlebars","Parent":2,"_destroy":false}] ;

None of the above elements will show. Even "_destroy": null has the same effect.

Working example of the problem...

http://jsfiddle.net/jy53e/6/

Update: Seems to be an issue with the mapping extension.

Upvotes: 0

Views: 333

Answers (1)

RP Niemeyer
RP Niemeyer

Reputation: 114792

What is happening is that you are sending _destroy through the mapping plugin and it is coming out as an observable. Knockout does not expect it to be an observable (a function), so when it does a check like if (_destroy) the result will be true, because _destroy is a function and it is not unwrapped to see its value.

You can do something like: http://jsfiddle.net/rniemeyer/jy53e/7/ to prevent _destroy from getting made into an observable.

So, use mapping options:

var mappingOptions = {
    create: function(options) {
        return ko.mapping.fromJS(options.data, { copy: ["_destroy"] }); 
    }
};

Upvotes: 5

Related Questions