Skip to content Skip to sidebar Skip to footer

Can I Make Knockout Generate Bindings From The Values In Other Bindings?

After asking this question and getting an answer I have a modal form which has its contents data bound to a knockout model. this is great and makes it generic and reusable, so lon

Solution 1:

I would handle this with a dynamic template, as described in note 5 here. So it would look something like this:

modal = {
    header: ko.observable("This is a modal"),
    //this is now just the name of the template
    body: ko.observable('bodyTemplateA'),
    // ...
};

And then in your binding, do

<divclass="modal-body"data-bind="template: { name: body }"></div>

and then of course define all of your needed templates separately:

<scriptid="bodyTemplateA"type="text/html">Name:<inputtype="text"data-bind="value: paramName" /><br/>Type:<inputtype="text"data-bind="value: paramType" /><br /></script>

Not quite what you were looking for, but I think that'll be a lot easier and maintainable than trying to keep all your various ko-bound html in strings throughout your code.

Post a Comment for "Can I Make Knockout Generate Bindings From The Values In Other Bindings?"