knockout.js

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) ]),

Knockoutjs template foreach, special first item

天大地大妈咪最大 提交于 2020-01-02 00:55:30
问题 So i have been busting my brain figuring this out. I have a foreach, spitting out templates, and i want the first element to have a special attribute. So far solutions i have found havent worked. This is the foreach: <h3 class="question">Geografi</h3> <p class="answer" data-bind="template: { name: 'geographyTmpl', foreach: geographyList, templateOptions: { selections: selectedGeographies } }"></p> This is the template: <script id="geographyTmpl" type="text/html"> <input class="geoCheckList"

Add CSS Class with Knockout Validator

北战南征 提交于 2020-01-02 00:49:13
问题 I want to add a CSS Class to a select element in my view, my view model has a property which I've extended using Knockout-Validation: self.selectedRootCause = ko.observable().extend({ required: true }); Then my select is like so: <form data-bind="submit: closeComplaint" method="post"> <select data-bind="options: rootCauses, optionsText: 'RootCauseText', value: selectedRootCause, optionsCaption: 'Choose..', validationOptions: { errorElementClass: 'input-validation-error' }"> </select> <input

Setting default values for computed Observable Knockout.js

久未见 提交于 2020-01-01 19:58:14
问题 I am new to Knockout.js I have 3 fields in the UI . Product value. Quantity Total Everything works fine with the computed observable and could save the data.The total will be changed in the backend for some business reasons. While retrieving the data back,I need to show the total from the DB as initial value,but when the user chnages the product and value ,the original computed function should be used. I tried bindingHandlers but could not get it right.. Help would be highly appreciable. var

Making a javascript knockout viewmodel without the new keyword

╄→гoц情女王★ 提交于 2020-01-01 18:30:15
问题 I'm looking through the knockout tutorials, and all the examples create the view model using the 'new' keyword: //from learn.knockoutjs.com function AppViewModel() { this.firstName = ko.observable("Bert"); this.lastName = ko.observable("Bertington"); this.fullName = ko.computed(function() { return this.firstName() + " " + this.lastName(); }, this); } ko.applyBindings(new AppViewModel()); I'm trying to avoid using the new keyword, which usually works perfectly ok, but I find trouble getting

SignalIR and KnockoutJS in Asp.Net Web Form

主宰稳场 提交于 2020-01-01 17:10:12
问题 I seen may samples of SignalIR and KnockoutJS samples on MVC platform but not on WebForm. Please suggest me, can we use on WebForm? Any articles link would be appreciable. 回答1: I know this is exactly a month late but here's a quick example [this is a trivial example that i have built from exploring MVC examples] lets say u have a page called MyPage in the .aspx file write the following: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %> <!DOCTYPE html

Computed stops triggering forever if dependency is inside of false branch statement

瘦欲@ 提交于 2020-01-01 16:46:08
问题 I'm faced a problem that my computed observable stops triggering after some sequence of dependency changes. Finally I found out the point: if dependency was inside of false branch statement during latest evaluation, computed will not be triggered next time even if condition became true before evaluation finished . Here is a sample: https://jsfiddle.net/sgs218w0/1/ var viewModel = new function(){ var self = this; self.trigger = ko.observable(true); self.fire = function(){ self.trigger(! self

Knockout.js secure binding

可紊 提交于 2020-01-01 15:06:49
问题 I want to use secure binding with knockout. to do so I use knockout-secure-binding.js. Who could explain why the following code does not work? it throws an error ` Uncaught #< Object > knockout-secure-binding.js:74` after the line ko.applyBindings(new viewModel()); <html> <head> <title></title> <script src="scripts/knockout-3.0.0-min.js"></script> <script src="knockout-secure-binding-master/dist/knockout-secure-binding.js"></script> </head> <body> <button type="button" data-sbind="sbtnClick"

Knockout DateTime Picker - Default Date not Binding

烈酒焚心 提交于 2020-01-01 14:24:36
问题 I am using this Bootstrap DateTime Picker: http://eonasdan.github.io/bootstrap-datetimepicker/ I have created a Custom Binder for DateTime called datepicker: ko.bindingHandlers.datepicker = { init: function(element, valueAccessor, allBindingsAccessor) { //initialize datepicker with some optional options var options = allBindingsAccessor().datepickerOptions || {format: 'DD/MM/YYYY HH:mm'}; $(element).datetimepicker(options); //when a user changes the date, update the view model ko.utils

How to access the observables in custom bindings when using Knockout-ES5

折月煮酒 提交于 2020-01-01 11:49:24
问题 If the model properties are ko.observable(), these can be accessed as below within the custom binding. var observable = valueAccessor(); When using Knockout-ES5 plugin how to get hold of the observable within the custom binding? Check the code below and look for comment "How to get propertyName here?" JS Fiddle when not using Knockout-ES plugin courtesy of Another Look at Custom Bindings for KnockoutJS Updated fiddle with model changed to use Knockout-ES plugin ko.bindingHandlers.datepicker =