Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a web app that allows users to submit and save HTML. The user types in the html s/he wants to use into several different fields. Some are text input elements and some are textarea elements. When the user submits their markup I convert the special characters into html entities.

When the user comes back to edit the html they submitted I fetch their entries from the data base and prepopulate the form using knockout. In order to display the html as they typed it, I call a function on any data that being bound to my view model to remove change any entities back into ampersands and brackets and all that fun stuff.

var viewModel = {
property : ko.observable(decode_entities(varContainingUserInput)),
....
}

I know when binding to most html elements like divs or p tags, you can use knockouts html binding to decode entities before you print them. This alleviates the need to call the decode_entities function. However this doesn't work with input fields.

I want to get rid of the decode_entities function, or roll it into knockout so it's not scattered all throughout my code. I would like to get knockout to just decode entities in my bindings with any extra work.

share|improve this question

1 Answer 1

You can extend the observable (http://knockoutjs.com/documentation/extenders.html) take a look at the numeric extender in particular.

you can also use the custom binding, but i think that extending the observable will be a better solution for this case.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.