knockout-2.0

Converting the form of an observable array and displaying in select list

蹲街弑〆低调 提交于 2020-01-17 06:23:42
问题 I have an observable array as below.. var myObservableArray = ko.observableArray(["Bungle","George","Zippy"]); I wish to copy the elements into another observable array but in the following format where type is always unknown.. var anotherObservableArray = ko.observableArray([ { name: "Bungle", type: "Unknown" }, { name: "George", type: "Unknown" }, { name: "Zippy", type: "Unknown" } ]); If the original myObservableArray changes, anotherObservableArray also needs to change. Any help is

Dynamic composition using knockout and durandal doesn't work

北战南征 提交于 2020-01-16 00:40:26
问题 I have a strange problem while using Durandal/Knockout. In some cases the binding doesn't work properly. I've simplified my situation which came in this question. I'm setting composition info somewhere in my code like: compositionInfo({ model: viewModelInstance, view: viewName, activate: viewModelInstance.activate }); And this is my view: <div id="service-container" data-bind="compose: { model: compositionInfo().model, view: compositionInfo().view, activate: compositionInfo().activate}"> At

knockout js foreach binding is not updating when used in custom element (components.register)

此生再无相见时 提交于 2020-01-15 12:13:43
问题 ko.components.register("toast-container", { viewModel: ToastViewModel, template: '<div data-bind="foreach: array1"><div data-bind="text:someText"> </div></div>' }); function ToastViewModel(params) { var self = this; self.array1 = ko.observableArray(); self.Onclick = function () { self.array1.push({ someText: "some content" }); } } $(function () { ko.applyBindings(new ToastViewModel()); }); When i push some content in array1 dynamically, the UI is not updating. Please help me. 回答1: Check this:

How to carry the data from one viewModel to another ViewModel Knockout JS

半世苍凉 提交于 2020-01-10 05:39:08
问题 I am using the Knockout js in my single page Application,I need to carry the value of one viewmodel data to another viewModel data,So i can reduce my duplication creating same view, How i can achieve this below is my code.I got 2 different js file,which one consist of Employee ViewModel and in another Department View Model //Employee View <div class="Employee-view" data-role="view" id="employee"> <div data-role="content" > <ul> <li foreach:EmployeeData> //Onlcick of this need to navigate to

see all extenders and custom bindings for observable

淺唱寂寞╮ 提交于 2020-01-06 03:08:37
问题 Is it possible to see all extenders and bindings attached to an observable within Knockout JS? Sample View Model: var viewModel = function(){ var self = this; self.firstName = ko.observable().extend({required: "Please enter a name", logChange: "first name" }); self.lastName = ko.observable().extend({ required:true}); } I am also using several custom bindingHandlers including the Knockout X-Editable Plugin in addition to the KO Validation Plugin Sample Multi-Page View: <!--Screen 1 --> <input

Under what conditions does a Knockout native template re-render

大憨熊 提交于 2020-01-05 11:02:25
问题 I have a complex knockout page that renders a template conditionally: <!-- ko template: {'if': $root.itemToEdit.SomeObject() === $data, name: 'EditItemTemplate', afterRender: $root.initializeEditPanel } --> <!-- /ko --> and the template: <script type="text/html" id="EditItemTemplate"> <div id="editContainer" class="fd_editContainer"> //.. lots of markup and knockout bindings ... <input class="checkbox" id="questionDisplayOptionOverride" type="checkbox" data-bind="checked: $data.AnObject()

Knockout with repeat binding: Initially selected value being overwritten

浪尽此生 提交于 2020-01-05 08:52:16
问题 I have a preset value for a select selectedValue which has a value of "ham". I have 3 options "spam", "ham", "cheese". When the viewmodel is applied, the "ham" value is selected , but the selectedValue looses it's value, so "ham" isn't actually selected although it appears to be. What do I need to change to allow for selectValue to retain it's initial value? Here's the jsfiddle Html <select data-bind="value:selectedValue"> <option data-bind="repeat: values" data-repeat-bind="value: $item(),

Detect if a change occur in observableArray

狂风中的少年 提交于 2020-01-05 07:11:09
问题 Does anyone have idea, if it's possible to detect whenever a change occur inside observableArray while using ko mapping, this mean without having to create the model by hand? self.items = ko.observableArray([]) var data = ko.mapping.fromJS(result) self.items.push(data); I would like to log any changes occurred in any property inside my array of objects. Thanks 回答1: If I'm not mistaken, you should be able to use subscribe on the observable to get that information. See the bottom of this page

Knockout.js: clear selection in select element

℡╲_俬逩灬. 提交于 2020-01-02 08:58:25
问题 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

knockoutjs how to get the selected option arrayObject

穿精又带淫゛_ 提交于 2020-01-02 03:57:25
问题 I want to get the selected option object <select data-bind="options: availableCountries, value: selectedCountry, event: { select: onSelect}"></select> <script type="text/javascript"> // Constructor for an object with two properties var Country = function(name, population) { this.countryName = name; this.countryPopulation = population; }; var viewModel = { availableCountries : ko.observableArray([ new Country("UK", 65000000), new Country("USA", 320000000), new Country("Sweden", 29000000) ]),