Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout.js - javascript function on data-bind

Tags:

Is there a way i can call JavaScript function on data-bind like this:

<span id="lblSomePropVal" data-bind="text: MySomeFunction(SomeProperty())" ></span> 

What i am trying to do is call MySomeFunction with the value of SomeProperty of my viewmodel. My SomeFunction will return some text based on value passed and that will be displayed in the span lblSomePropVal.

I tried it the way i have written in the example but it throws binding error.

Am i missing something here or is there any other way of doing this?

This is the error i am getting:

Microsoft JScript runtime error: Unable to parse bindings. Message: [object Error]; Bindings value: text: MySomeFunction(SomeProperty()) 
like image 914
Asdfg Avatar asked Mar 02 '12 16:03

Asdfg


People also ask

What is data binding in KnockoutJS?

Knockout's declarative binding system provides a concise and powerful way to link data to the UI. It's generally easy and obvious to bind to simple data properties or to use a single binding. For more complex bindings, it helps to better understand the behavior and syntax of Knockout's binding system.

How do I get KnockoutJS data?

prototype. loadNote = function (id) { var self = this; $. getJSON(uri + '/' + id, function (data) { self. note(data); }); }; // Apply bindings ko.

How do you bind data in JavaScript?

Data binding in concept is quite simple. On one side, you have a data model and on the other side, you have an interface, often called a view. The idea is that you want to “bind” some piece of data to something on the view so that when the data changes, the view changes. This is typical for read-only data.


1 Answers

I had a similar problem trying to calculate table cell entries. What worked for me was including 'MySomeFunction' in my data model, and then data-binding my table cells as:

<td data-bind="text: $root.MySomeFunction(SomeProperty)"></td> 
like image 133
ProfNimrod Avatar answered Sep 29 '22 12:09

ProfNimrod