coderenaissance / knockout.viewmodel

The knockout viewmodel plugin is the fastest, smallest, cleanest, most flexible way to create a knockout viewmodel.
http://coderenaissance.github.com/knockout.viewmodel
106 stars 28 forks source link

Why Nested Property is Not Observable #70

Closed Yigmoryar closed 5 years ago

Yigmoryar commented 6 years ago
var model = {
    movies:[
        {
            id:256889,
            name:"item Name",
            description:"Description of Item",
            showtimes:[
                {start:"11:45am", duration:"120 minutes", soldOut:false, pricingType:"Matinee"},
                {start:"6:45pm", duration:"120 minutes", soldOut:false, pricingType:"Evening"}
            ]
        }
    ]
};

var options = { 
    arrayChildId:{//child item id
        "{root}.movies":"id"
    },
    extend:{
        "{root}.movies[i]":"ExtendWithSelectable",//Reference shared function by name
        "{root}.movies[i].showtimes[i]": function(showtime){

            //Reference shared function directly
            options.shared.ExtendWithSelectable(showtime);

            showtime.price = ko.computed(function(){
                if(showtime.pricingType = "Matinee"){
                    return 6.75;
                }
                else if(showtime.pricingType = "Evening"){
                    return 10.25;
                }
            });
        }
    },
    shared:{
        ExtendWithSelectable:function (item){
            item.selected = ko.observable(false);
            return item;
        }
    }
}

var viewmodel = ko.viewmodel.fromModel(model, options);
  1. after top code execute, the nested property pricingType would not be observable.
  2. when comment code below, the nested property pricingType would be observable.
            // showtime.price = ko.computed(function(){
            //     if(showtime.pricingType = "Matinee"){
            //         return 6.75;
            //     }
            //     else if(showtime.pricingType = "Evening"){
            //         return 10.25;
            //     }
            // });

so, is this a bug? I'll appreciate if anyone could tell me what happened, thanks.

miellaby commented 6 years ago

You made a basic Javascript error, this is not: if(showtime.pricingType = "Matinee") but if(showtime.pricingType == "Matinee") and so on.

Yigmoryar commented 6 years ago

thx. I'm so ashamed.