knockout-2.0

Showing a concatenated string from multiple values of observable array

时光毁灭记忆、已成空白 提交于 2019-12-11 01:58:25
问题 I have an observable array as follows.. var myObservableArray = ko.observableArray([ { name: "Bungle", type: "Unknown" }, { name: "George", type: "Unknown" }, { name: "Zippy", type: "Unknown" } ]); I wish to fill a select listbox from elements in the observable array such that the option names are concatenated string of 'name' and 'type' like "Bungle-Unknown","George-Unknown",.etc The optionvalues are just 'name'. Any help is sincerely appreciated. Thanks 回答1: In your binding on the select

Best Practice with Multiple ViewModels

两盒软妹~` 提交于 2019-12-11 01:54:46
问题 I have an edit page with 3 logical parts to the page: Header Order Payment The user is allowed to edit each section at once and submit the changes with a single submit button. This page is rather large in terms of functionality and the object the user is editing. The object being sent to the page for edit is a JSON object with some properties that don't get edited but need to pass back on submit. Is the best practice in this scenario to have the following VM structure: Master VM Header VM

Custom binding for when foreach has finished rendering

大兔子大兔子 提交于 2019-12-10 23:18:50
问题 I have an observable array with items bound to an ul . I need to calculate the width of the entire set of li items once the items have been added so I can set the width of the ul element to the total width of the child li elements. How can I go about doing this with a foreach binding? <div> <h2 data-bind="visible: items().length">Test</h2> <ul data-bind="foreach: { data: items, afterAdd: $root.myAfterAdd }"> <li data-bind="text: name"></li> </ul> </div> And the JavaScript: var viewModel = {

Durandal recursive composing of template

点点圈 提交于 2019-12-10 16:46:05
问题 I am trying to compose a template in my application recursively. I have a multi-dimensional array that I am trying to flatten out into table rows. I can get the first level to work great, but I cannot get subsequent levels to render properly. I know that Durandal requires the view to have a single root element. I am using a virtual container to compose my template. Here is my parent view <table class="table table-bordered table-hover"> <thead> <tr> <th>#</th> <th></th> <th>Condition</th> <th

Knockout.js “if Binding” on multiple booleans

吃可爱长大的小学妹 提交于 2019-12-10 12:28:00
问题 Is it possible to use Knockout's if binding on more than one boolean? Such as <div data-bind="if: (property.aTrueValue && property.anotherTrueValue)">... I've tried a lot of different syntax, but can't seem to find the right syntax. I'm not sure it's even possible. 回答1: When Knockout processes your bindings it first evaluates your expression. If the expression results in an observable, it then evaluates the observable as a convenience to get the final value that the if: works on. So the two

Replace all elements in Knockout.js observableArray

女生的网名这么多〃 提交于 2019-12-09 07:24:19
问题 I have an observableArray in my view model. After creating the vm I wish to completely replace the data the observableArray . Here's how I'm doing it: //Initial Setup var vm = {}; vm.roles = ko.observableArray([]); ko.applyBindings(vm); //....replace array later on.... vm.roles(["1", "2"]); This seems to be working fine, but I was concerned if this was incorrect and might lead to memory leaks. Can anyone conform if this is the preferred way to update an existing observableArray assuming you

How to Clear Contents of an observableArray That was Populated from Previous Visits to a View

最后都变了- 提交于 2019-12-09 05:00:32
问题 I have a Single Page Application that uses knockout for the data binding. The CAApproval.html view in my single page application has an observeablearray named AllCertificates in the viewmodel code. It populates fine on the page. When you navigate away from the view by clicking a link in the navigation.html part of the page and then return to CAApproval page, the values from the previouse visit are still in the AllCertificates observableArray and therefore are displayed on the CAApproval view.

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

前提是你 提交于 2019-12-09 03:16:27
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 viewModel = { Name: ko.observable("@Model.Name"), EmailLabels: ko.observableArray(@Html.Json(Model

is ko.applyBindings synchronous or asynchronous?

核能气质少年 提交于 2019-12-09 03:09:16
问题 Does the generated view exists right after you call ko.applyBindings() or does the scaffolding happen asynchronously? Thanks! 回答1: ko.applyBindings is a synchronous call. There may be cases where bindings have special code to do things in a setTimeout, but this is not generally the case. With the addition of components in Knockout 3.2, components are asynchronous. With Knockout 3.3, there will be an option to render components synchronously if the viewmodel / template is loaded. 回答2: Knockout

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

≯℡__Kan透↙ 提交于 2019-12-08 09:56:25
问题 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