Using functions of items in jQuery templates

I'n using knockout js and jQuery templates. So i want to render some model with name and date. Problem is in Date field. I've wrote function that print date in my language natural form, and want to use it in template.

I've wrote this template:

<script type="text/html" id="item-template">
    ${Name} (${Date().toRussianDateString()})
</script>

but recived error Uncaught SyntaxError: Unexpected token )

So i've found this solution:

<script type="text/html" id="item-template">
    ${Name} (${$data.Date().toRussianDateString()})
</script>

of using $data and $item variables. Is right solution? Can i wrote this template without using $date and $item?

Thanx.

Answers


It's a little dirty because you're mixing the dead-in-the-water jquery.tmpl engine with native KO templates.

If you're able to use the latest version of KO, you can just write this:

<div data-bind="foreach: items">
    $data.Name ($data.Date().toRussianDateString())
</div>

Notice that is inline; you don't need any separate script block or named templates.


Need Your Help

What is the best way to put a translation system in php website?

php localization translation

I'm developing a website in PHP and I'd like to give the user to switch from German to English easily.

is it possible to set a “watch” on an HTML element such that it hits a javascript debugger breakpoint when the HTML element changes?

javascript jquery debugging watch

Is it possible to set up a "listener" of some sort on an HTML element such that when this element is changed (perhaps by javascript / jQuery) the javascript debugger breaks on the line of code that

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.