1

I got couple question with the following code

  1. Does the TokenList in PromoTemplate a observableArray, what happened to each item in the array ? are they automatically gets wrapped into observable?

  2. All the binding seems to work, i am be able to get all the way down to a Token's value, but when I modify the data in a input it doesn't seem to notify other controls that are bound to the same property, for example the AttributeToken.Value.

    function AttributeToken(data) {
        this.Identifier = ko.observable(data.Identifier)
        this.DataType = ko.observable(data.DataType)
        this.Value = ko.observable(data.Value);
    }
    
    function PromoTemplate(data) {
        this.Identifier = ko.observable(data.Identifier);
        this.Name = ko.observable(data.Name);
        this.Content = ko.observable(data.Content);
        this.TokenList = ko.observableArray(data.Tokens);
        this.Sample;
    }
    
    function PromoTemplateViewModel() {
        // Data
        var self = this;
        self.promoTemplates = ko.observableArray([]);
    
    
        self.selectedPromoTemplate = ko.observable();
    
        // Init
        $.getJSON(promoTemplatesUrl, function (allData) {
            var mappedPromoTemplates = $.map(allData, function (item) { return new PromoTemplate(item) });
            self.promoTemplates(mappedPromoTemplates);
        });
    
    }
    

the json data look like the following

[{"Content":"<product><data price = \"100\" discountPercentage=\"{Percentage}\" startDate=\"{StartDate}\" ><\/data><\/product>","Description":null,"Identifier":"1","Name":"Percentage Promo","Tokens":[{"DataType":"double","Identifier":"{Percentage}","Value":"20"},{"DataType":"date","Identifier":"{StartDate}","Value":"10\/21\/2012"}]},{"Content":"<product><data price = \"250\" discountAmount=\"{DiscountAmount}\" startDate=\"{StartDate}\" ><\/data><\/product>","Description":null,"Identifier":"2","Name":"Dollar off Promo","Tokens":[{"DataType":"integer","Identifier":"{DiscountAmount}","Value":"5"},{"DataType":"date","Identifier":"{StartDate}","Value":"10\/21\/2012"}]}]

1 Answer 1

2

observableArrays do not automatically make all of the properites of the items that they hold observable. An observableArray will only notify subscribers when you manipulate the array itself (push, pop, slice, etc.) or replace the array entirely.

So, in your case, you would need to map data.Tokens to AttributeToken instances in a similar manner to how you are mapping the overall data or look at using something like the mapping plugin.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.