0

Having an array of different types of items:

var items = [{{type:t1,text:"abc"},{type:t2,data:"123"}}]

and view models:

function T1VM(t1) { ... }
function T2VM(t2) { ... }

How can I use the mapping plugin to map items to an observable array of matching view models?

2
  • What do you mean a "matching view model"? Do you want the first view model mapped to the second? Or the second to the first? Or do you have a third view model you want the other two mapped to? Commented Mar 30, 2013 at 11:55
  • Objects with type: "t1" should be mapped to T1VM etc... Commented Mar 30, 2013 at 13:20

1 Answer 1

1

It was too easy!

var mapping = {
    create: function (options) {
        if (options.data.type == "T1")
            return new T1VM(options.data);
        else if (options.data.type == "T2")
            return new T2VM(options.data);
    }
};

And then:

ko.mapping.fromJS(items, mapping);
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.