knockout.js

knockout $data binding to an HTML element

你离开我真会死。 提交于 2019-12-24 18:12:04
问题 So, suppose I have this viewmodel named people which consists of an array of person object literals , like [{ name = John, age = 30, sex = male }, { name = Mike, age = 29, sex = male }, { name = Anna, age = 28, sex = female }] And suppose I wanted to data-bind each person to an <li> , like <ul data-bind="foreach: people"> <li data-bind="text: name"></li> </ul> But , is it possible, maybe through data-bind="with: $data" , to bind the whole person object to the <li> so, for example, when I

knockout $data binding to an HTML element

瘦欲@ 提交于 2019-12-24 18:11:00
问题 So, suppose I have this viewmodel named people which consists of an array of person object literals , like [{ name = John, age = 30, sex = male }, { name = Mike, age = 29, sex = male }, { name = Anna, age = 28, sex = female }] And suppose I wanted to data-bind each person to an <li> , like <ul data-bind="foreach: people"> <li data-bind="text: name"></li> </ul> But , is it possible, maybe through data-bind="with: $data" , to bind the whole person object to the <li> so, for example, when I

Synchronizing a button class with an option control using knockoutjs

时光总嘲笑我的痴心妄想 提交于 2019-12-24 18:01:06
问题 usually I can figure out a way to make Knockout-js do what I want. In this case however, i'm struggling a little, so I'm opening the question up to the community here on SO. Introduction I'm writing an HTML5 web app using typescript, bootstrap, knockoutjs and a nodejs backend. In my UI which is all controlled via knockoutJS I have a set of buttons, formed as a bootstrap 3 button group of select-able options. This justified group, gives me 4 buttons horizontally, but allows the behaviour of

Knockout validation not firing with knockout custom binding

*爱你&永不变心* 提交于 2019-12-24 17:13:07
问题 I have a knockout custom binding handler for an input that I need to have validated by knockout-validation. However nothing I've done has worked. Validation is not fired. I can get validation firing on a plain ko value binding on an input bound to the same viewmodel property. What I've found from my research (ko docs, ko-validation github docs and this SO question validationOptions not working with custom bindingHandlers amongst many others) is that you need: ko.validation

Knockout subscribe is blocking the page

故事扮演 提交于 2019-12-24 17:09:11
问题 I have an observable array with a computed to get the currently selected item of a radio list. self.KOVehicle.SelectedPolicy = ko.computed(function () { return ko.utils.arrayFirst(self.KOVehicle.VehicleDetailsList(), function (veh) { return veh.PolicyId() == self.KOVehicle.SelectedPolicyId(); }); }, self); I'm subscribing to changes on that which will update a dozen or so various bound items on the page. self.KOVehicle.SelectedPolicy.subscribe(function (selectedPolicy) { // show ajax loading

how to? with knockout js validations

蹲街弑〆低调 提交于 2019-12-24 16:41:54
问题 I've started using knockout js validations with http://ericmbarnard.github.com/Knockout-Validation/ validation engine, and I'm not clear as to how to do the following: 1) Say I want to set a particular field required based on a condition. How do I do that? e.g. this.Username = ko.observable().extend({ required: true }); // make required = true only if this.UserType = 2, etc... 2) I've got the validation messages firing right next to the field being validated. I want only an '*' to appear next

Knockout component fails if element removed before template returned

与世无争的帅哥 提交于 2019-12-24 16:26:54
问题 I have a KO custom binding which adds a component to the page (as a virtual element but i don't think this matters) and then applies a view model to it. The component loads it's template via require from the server. However during load I am getting a problem where the custom binding is updated and removing the element from the page (I want to have it tidy it's self up if not required). This results in a race condition - if the asychronous lookup for the template has not finished before the

trigger computed to be recalculated when observableArray element property is changed

别说谁变了你拦得住时间么 提交于 2019-12-24 16:24:39
问题 I have observableArray with elements (elements are not observables, but some unobservable objects). I have also computed variable that depands on observableArray . If I push elements into array, then everything works (I mean - computed variable is recalculated). However, if I change some properties of observableArray single items, then computed is not recalculated: var myObsArr = ko.observableArray(); // myObsArr is loaded using ko.mapping.fromJS with data from server // but I am using custom

Is it safe to call ko.applyBindings without any parameters?

风格不统一 提交于 2019-12-24 16:03:08
问题 This is the only official doc I could find on ko.applyBindings(): http://knockoutjs.com/documentation/observables.html It's not a real formal written document that says exactly what is optional/etc. In testing it appears that calling ko.applyBindings() allows for bindings on a global scope, and it appears to work fine. Has anyone that studied the source code (or know more about KO than I do), know if this is safe or not? Any performance issues? This is the start of my "app" object for an SPA

KnockoutJS select fires onChange after setting default value

拟墨画扇 提交于 2019-12-24 15:53:50
问题 I have set up the following select for changing semesters on page. When the select detects a change, the changeSemesters function is fired, which runs an AJAX that replaces the current data on the page with data specific to the selected semester. View <select data-bind="options: semestersArr, optionsText: 'name', optionsValue: 'id', value: selectedSemester, event: { change: changeSemesters }"></select> ViewModel this.selectedSemester = ko.observable(); //runs on page load var getSemesters =