Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining text and html by Knockout

This is how i want to construct my html

<a href="#">John <i class="person"></i></a>

And with knockout.js this is what i have done.

<a data-bind="text:name"><i class="person"></i></a>

As you can guess whole elements(not just text) of anchor is removed becuse of text binding in this case whole tags inside of anchor is removed. My solution is at below.

<a data-bind="html: name() + '<i class="person"></i>'"></a>

string concat with html in data-bind is a solution but it has 2 big downside. 'name' propery is not safe so we can got html injection. Sedondly writing html inside of data-bind attributes is sucks.

Another solution is.

<a href="#"><span data-bind="text:name"></span><i class="person"></i></a>

I know we introducing new html markup for just solution. It is what i have found best.

What is the well known solution for this problem in knockout.js ?

Can we specify to just update text not whole elements inside of it to text binding via parameters ?

Or better solution ?

like image 508
Freshblood Avatar asked Aug 24 '12 12:08

Freshblood


People also ask

How do I use KnockoutJS in HTML?

It is very easy to use KnockoutJS. Simply refer the JavaScript file using <script> tag in HTML pages. A page as in the following image will be displayed. Click on download link and you will get the latest knockout.

What is text knockout?

When white text is set on a black background, the text is 'knocked out' and the paper shines through, hence the term 'knockout text'. Reversed type doesn't have to be white. The term is also used for text with a light color on a darker colored background. Reversed type is often used to emphasize text.

What is two way binding in KnockoutJS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.

What is KnockoutJS used for?

KnockoutJS is basically a library written in JavaScript, based on MVVM pattern that helps developers build rich and responsive websites.


2 Answers

Using the span is the preferred solution. If the text binding doesn't replace all of the content, then it is hard for it to know what to update and not update the next time that it changes. If you want to always deal with the first child node of the element, then you could write a small custom binding to help.

Here is a simple prependText binding. This would always replace the first child node of the element that contains the binding. So, you would want to ensure that the first node was at least a space.

ko.bindingHandlers.prependText = {
    update: function(element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        //replace the first child
        element.replaceChild(document.createTextNode(value), element.firstChild);
    }        
};

Use it like:

<a href="#" data-bind="prependText: name"> <span> another element</span></a>

Sample: http://jsfiddle.net/rniemeyer/5CfzH/

like image 112
RP Niemeyer Avatar answered Oct 14 '22 00:10

RP Niemeyer


You can also use KO "containerless" notation

<!-- ko text: YourProperty -->
<!-- /ko-->

The same can be done with other bindings (such as foreach): See Part 4

like image 24
mrtig Avatar answered Oct 14 '22 02:10

mrtig