knockout-2.0

Knockout.js Extending value binding with interceptor

♀尐吖头ヾ 提交于 2019-11-27 11:48:51
问题 This seems to be a common approach to sanitizing/validating/formatting data with knockout when binding to an input field, it creates a reusable custom binding that uses a computed observable. It basically extends the default value binding to include an interceptor that will format/sanitize/validate input before written/read. ko.bindingHandlers.amountValue = { init: function (element, valueAccessor, allBindingsAccessor) { var underlyingObservable = valueAccessor(); var interceptor = ko

How to get Selected Text from select2 when using <input>

允我心安 提交于 2019-11-27 10:14:14
问题 I am using the select2 control, loading data via ajax. This requires the use of the <input type=hidden..> tag. Now, I want to retrieve the selected text. (The value property in the data-bind expression sotres the id only) I have tried $(".select2-chosen").text() , but this breaks when I have multiple select2 controls on the page. 回答1: As of Select2 4.x, it always returns an array, even for non-multi select lists. var data = $('your-original-element').select2('data') alert(data[0].text); alert

make an input only-numeric type on knockout

杀马特。学长 韩版系。学妹 提交于 2019-11-27 04:14:52
i read many tutorials but i dont know how to do this, this is the input input(type="text",name="price",id="price"data-bind="text: price,valueUpdate:['afterkeydown','propertychange','input']") and this is my viewModel price: ko.computed(function() { return parseFloat(this.replace(' ','').replace(/[^0-9\.]+/g,"")) || ''; },this) but this cause error: this has no method replace??? how can i pass the price value to the computed function?? Martin Surynek Is better to create custom binding http://knockoutjs.com/documentation/custom-bindings.html which accept only allowed characters [0-9,.] as

Is there a reason I would use Knockout MVC instead of Knockout JS?

♀尐吖头ヾ 提交于 2019-11-27 04:13:36
问题 Another user suggested Knockout MVC to handle some AJAX posting issues. I read a little on it and I see it is a wrapper around Knockout JS. So I wonder what are the real differences between the two? Should I bother with Knockout JS since Knockout MVC exists? When would I use one over the other? 回答1: Knockout MVC is the bastard child of WebForms . It routes all viewmodel methods through controller actions, meaning everything that happens has to bounce to the server and back. I cannot

Knockoutjs (version 2.1.0): bind boolean value to select box

北城余情 提交于 2019-11-27 02:33:57
问题 I want to bind boolean value to select element using KO v2.1.0, but obviously it doesn't work as expected. html code: <select data-bind="value: state"> <option value="true">On</option> <option value="false">Off</option> </select> Javascript code: var model = { state: ko.observable(false) }; ko.applyBindings(model) So I expect the select box goes to "Off" position with the initial value false but it was at "On". If I put state: ko.observable("false") it will be correct but that's not I wanted.

Subscribe to observable array for new or removed entry only

这一生的挚爱 提交于 2019-11-27 01:17:19
问题 So yes I can subscribe to an observable array: vm.myArray = ko.observableArray(); vm.myArray.subscribe(function(newVal){...}); The problem is the newVal passed to the function is the entire array. Is there anyway I can get only the delta part? Say the added or removed element? 回答1: As of KnockoutJS 3.0, there's an arrayChange subscription option on ko.observableArray. var myArray = ko.observableArray(["Alpha", "Beta", "Gamma"]); myArray.subscribe(function(changes) { // For this example, we'll

Knockout is not evaluating an expression when using $index in a binding

こ雲淡風輕ζ 提交于 2019-11-26 21:47:02
问题 Why is it, that when I try to use knockout.js to bind some text using $index, I get the code of a function instead of a number? <tbody data-bind="foreach: MyList"> <tr> <td><span data-bind="text: $index + 1"></span></td> </tr> </tbody> Instead of getting 1, 2, 3 etc., I get this: You can see, by the last character in the above image, that my index of zero is being added to 1. If I remove the '+ 1' from my binding, I get 0, 1, 2 instead of the function. How do I tell knockout to evaluate the

Knockout.js Make every nested object an Observable

不羁的心 提交于 2019-11-26 19:28:36
问题 I am using Knockout.js as a MVVM library to bind my data to some pages. I'm currently building a library to make REST calls to a web service. My RESTful web service returns a simple structure: { id : 1, details: { name: "Johnny", surname: "Boy" } } I have an observable main parent, myObject . When I do myObject(ko.mapping.fromJS(data)) the observables in myObject are: id name surname How can I make details (and theoretically any object in the structure an observable)? I need this behavior so

Knockout: computed observable vs function

不羁的心 提交于 2019-11-26 19:14:00
问题 When using knockout, what is the advantage of using read-only computed observables rather than simple functions? Take the following viewmodel constructor and html snippet, for example: ​ var ViewModel = function(){ var self = this; self.someProperty = ko.observable("abc"); self.anotherProperty = ko.observable("xyz"); self.someComputedProperty = function(){ return self.someProperty() + self.anotherProperty(); }; }; <input data-bind="value: someProperty"/> <input data-bind="value:

make an input only-numeric type on knockout

梦想与她 提交于 2019-11-26 12:43:26
问题 i read many tutorials but i dont know how to do this, this is the input input(type=\"text\",name=\"price\",id=\"price\"data-bind=\"text: price,valueUpdate:[\'afterkeydown\',\'propertychange\',\'input\']\") and this is my viewModel price: ko.computed(function() { return parseFloat(this.replace(\' \',\'\').replace(/[^0-9\\.]+/g,\"\")) || \'\'; },this) but this cause error: this has no method replace??? how can i pass the price value to the computed function?? 回答1: Is better to create custom