knockout-2.0

Knockout and jQuery Validate

假装没事ソ 提交于 2019-12-08 08:00:08
问题 I've some problems getting KO and jQuery Validate plugin work together. My model: Parameter { int Id; string Name; decimal Price; } And my HTML+JS: <DOCTYPE html> <html> <head> <meta http-equiv='Content-Type' content='text/html;charset=utf-8'></meta> <script type='text/javascript' src='js/jquery-1.8.3.min.js'></script> <script type='text/javascript' src='js/jquery.validate.min.js'></script> <script type='text/javascript' src='js/knockout-2.2.0.js'></script> <script type='text/javascript' src=

Knockout view model posts back to ASP.NET MVC partially - how to post back complete object?

早过忘川 提交于 2019-12-08 05:57:37
问题 Having these ASP.NET MVC view models: public class User { public string Name { get; set; } public LabeledEmail LabeledEmail { get; set; } } public class LabeledEmail { public IList<ContactLabel> Labels; public IList<ContactEmail> Emails; } and Knockout view model like this: <script type="text/javascript"> $(function() { ko.applyBindings(viewModel); $("#profileEditorForm").validate({ submitHandler: function(form) { if (viewModel.save()) window.location.href = "/"; return false; } }); }); var

Knockout - write a value to a ko.computed

此生再无相见时 提交于 2019-12-07 13:31:57
问题 I've built a very data/number heavy app in Knockout. I'm currently getting the error: Uncaught Error: Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters. This is happening when my custom bindingHandler (which formats the numbers into 'large' form, ie. 123,345,678,987) tries to write back to the original input which displays the value of a computed function. Value displayed in an input element: self.value

Knockout bindingHandler for comma separated numbers with same entry again in a textbox

故事扮演 提交于 2019-12-06 14:53:29
I needed to create a Knockout bindingHandler which would format the amount to comma separated numbers. After a little searching I found the solution here (Thanks to @nemesv for the solution) which uses http://numeraljs.com/ for the conversion. the binder is as follows: ko.bindingHandlers.formatMoney = { init: function(element, valueAccessor) { var value = valueAccessor(); var interceptor = ko.computed({ read: function() { return numeral(ko.unwrap(value)).format('0,0.00'); }, write: function(newValue) { if($.trim(newValue) == '') value("0"); else value(numeral().unformat(newValue)); } }).extend

Force native Knockout Templating

不羁岁月 提交于 2019-12-06 06:48:37
问题 I have a page that requires jQuery.tmpl, but I want to use native knockout templating for a data-bind="foreach: Comments" attribute. Because I have included jQuery.tmpl, knockout's native templating is disabled; is there a way that I can force the native functionality? Thanks 回答1: You cannot use foreach or other control-flow bindings within a jQuery.tmpl template. However, if you want to call a named template and force it to use the native template engine, then you would do something like:

knockoutjs select change event gets fired when binding

自作多情 提交于 2019-12-06 03:38:59
问题 I have this knockout code: http://jsfiddle.net/nickbuus/Rwabt/ The problem is that whenever the dropdown select box gets filled the change event gets called: <select data-bind="options: $root.Categories, optionsText: 'categoryName', optionsValue: 'categoryId', value: CatId, optionsCaption: 'Vælg...', event: { change: $root.selectionChanged }"></select> How do I fix this ? 回答1: There was a lot of broken and unused code in there http://jsfiddle.net/Rwabt/5/ This one is corrected. What actually

Knockout.js: clear selection in select element

纵饮孤独 提交于 2019-12-06 03:13:07
I need to clear the selection from a <select> element. I've already read such posts as Knockoutjs clear selected value in combobox and have tried the accepted answers, but those solutions don't seem to be working (don't know if something has changed in Knockout 2 since the answer was accepted?). Here's an example view model: var ClearSelectionViewModel = function () { var self = this; self.station = ko.observable(); self.selectedStation = ko.observable(); self.selectedStation.subscribe(function (value) { self.station(value); }); self.stations = ko.observableArray(['CLT', 'PHL', 'PHX', 'PIT']);

knockout - execute code after the last item has been rendered

 ̄綄美尐妖づ 提交于 2019-12-06 00:39:29
问题 i'm using jquery quicksearch to search a table that is being populated by knockout foreach loop. the quicksearch element needs to be initiate after the foreach finishes . I've tried several approaches, but was unsuccessful so far. I've tries using 'afterRender', but was unable to determine if the current item is the last item on the collection, i've also tried using a bindingHandlers, but then i got a collection of length 0 instead of length 2005. so: what's the best approach to finding the

How can I bind a ko.observableArray of strings?

倖福魔咒の 提交于 2019-12-05 21:06:36
问题 I'm trying to bind a ko.observableArray of strings to a template, but I'm unable to get the template to pick up changes in the strings inside the array. If I bind a set of objects instead of a set of strings, I get updates to the JSON, but they don't trigger anything until I actually change the first, non-array value. I'd prefer to find an array of strings, however, as I will be able to post the data model directly back to the server without any post-processing. How can I get the updates to

How to build a textarea with character counter and max length?

强颜欢笑 提交于 2019-12-05 19:24:26
问题 Please consider this jsfiddle. It contains something along these lines: <textarea data-bind="value: comment, valueUpdate: 'afterkyedown'"></textarea> <br/><br/> <span data-bind="text: getCount, valueUpdate: ['afterkeydown','propertychange','input']"></span> characters??? And this JavaScript: var viewModel = function(){ var self = this; self.count = ko.observable(0); self.comment = ko.observable(""); self.getCount = function(){ var countNum = 10 - self.comment().length; self.count(countNum); }