knockout.js

Knockout: Error: You cannot apply bindings multiple times to the same element when refreshing page

[亡魂溺海] 提交于 2019-12-24 02:44:01
问题 I'm using Knockout to bind an MVC view. This works fine the first time but for subsequent refreshes Knockout throws the error: Error: You cannot apply bindings multiple times to the same element. This is my binding code (inside Document.Ready), note that I am using setTimeout to run every 25 seconds, this is where the error occurs: function viewModel() { Loading = ko.observable(true), CurrentUser = ko.observable(), Environments = ko.observableArray(), CurrentFormattedDate = ko.observable() }

Knockout + MVC - You cannot apply bindings multiple times to the same element

守給你的承諾、 提交于 2019-12-24 02:23:44
问题 My MVC view: @model MG.ViewModels.Profile.ProfileDetailsViewModel <div> <h4>About Me</h4> <!-- ko if: !isEditingAboutMe() --> <p data-bind="text: aboutMe()">@Model.AboutMe</p> @if (Model.CurrentUserCanEdit) { <a data-bind="click: editAboutMe">edit</a> } <!-- /ko --> <!-- ko if: isEditingAboutMe() --> @Html.TextBoxFor(model => model.AboutMe, new { data_bind = "value: aboutMe" }) <a data-bind="click: saveAboutMe">save</a> <a data-bind="click: cancelAboutMe">cancel</a> <!-- /ko --> </div>

Stop Knockoutjs validation during applyBindings (init/onload)

我们两清 提交于 2019-12-24 02:18:47
问题 How do I stop the validation from firing when the page first loads? I want the validation to fire when the textbox gets edited or has focus and loses focus but not when the page first loads. My Loading code: $(document).ready(function () { ko.validation.configure({ registerExtenders: true, messagesOnModified: false, decorateElement: true, errorClass: 'error', insertMessages: true, parseInputAttributes: true, messageTemplate: 'customMessageTemplate' }); ko.applyBindings(new UserAccount

avoiding TypeError: Converting circular structure to JSON

左心房为你撑大大i 提交于 2019-12-24 02:00:57
问题 I know this has been asked before a couple of times but I still can't quite find a solution to this issue. (I've only begun using KO 2 weeks ago.) I have a bunch of fields that are each bound to observables in my VM. When the user clicks the GO button, I want to convert all the fields entered into a JSON string and send to server. This is the ViewModel: function ViewModel() { var self = this; function searchVar() { self.users = ko.observable(); self.fromdate = ko.observable(); self.todate =

Knockout Kendo dropdownlist get text of selected item

情到浓时终转凉″ 提交于 2019-12-24 01:57:18
问题 My application is MVC 5. I am using the following Knockout Kendo dropdownlist: <input data-bind="kendoDropDownList: { dataTextField: 'name', dataValueField: 'id', data: foodgroups, value: foodgroup }" /> <hr /> Selected: <strong data-bind="text: foodgroup"> </strong> <script> var ViewModel = function () { var self = this; this.foodgroups = ko.observableArray([ { id: "1", name: "apple" }, { id: "2", name: "orange" }, { id: "3", name: "banana" } ]); var foodgroup = { name: self.name, id: self

MVC 4 Dynamic action method argument

蹲街弑〆低调 提交于 2019-12-24 01:45:31
问题 I have the following class definition: public class CallGroupViewModel { public string Name { get; set; } public string BulkEmails { get; set; } public List<dynamic> Members {get;set;} } The Members property is some dynamic knockout code and the contents change depending on UI stuff. I am wanting to take a short cut since the UI is so up in the air right now. My problem is that when it get to my action method, everything gets populate, including Members but its a array of objects and I can't

Knockout observable change not detected if value changed externally

梦想的初衷 提交于 2019-12-24 01:43:30
问题 I have a list of text boxes bound to the ko.observableArray . I have to make sure that text box values can't be blank, and I do it with jQuery by setting the value to 0 if it's blank on blur() The problem is that the value change done with jQuery is not registered by knockout. How do I observe the value change in my model? See my simplified fiddle to get the point across - http://jsfiddle.net/k45gd/1/ HTML <input type="number" data-bind="value: age" /> <span data-bind="text: age"></span>

Knockout.js mapping options being ignored on nested values

南楼画角 提交于 2019-12-24 01:38:06
问题 I am unable to get the knockout.js mapping options to work on nested values. With the following code, the first level "shouldBeCopied" works fine and the nested "shouldBeCopied" value is always an observable. var data = { shouldBeCopied: "copied", nested: { shouldBeCopied : "copied" } }; var vm = ko.mapping.fromJS(data, { 'copy': ["shouldBeCopied"], "nested": { 'copy': ["shouldBeCopied"] } }); console.log(vm); Any ideas? Here's a fiddle if anyone wants to play around with it. 回答1: When using

Change checkbox value on parent div click

岁酱吖の 提交于 2019-12-24 01:15:14
问题 I'm using knockout and trying to change the value of a checkbox when I click on the parent div. This works great but now I can't change the checkbox value when actually clicking on the checkbox...the span after the checkbox is for debugging purposes to see what the variable is set to. I tried using clickBubble: false but that didn't seem to help either. I have the following mapping: var mapping = { 'teams': { create: function(options) { options.data.active=ko.observable(false); options.data

KnockoutJS options binding with duplicate values

百般思念 提交于 2019-12-24 00:58:04
问题 I'm building a simple app with Knockout.js as a proof of concept. As I'm very new to Knockout, the code in this question may be far from perfect and exhibit bad practices, so feel free to let me know if that's the case! I'm using the options binding to produce the contents of a select element: <select data-bind="options: titles, optionsText: 'display', optionsValue: 'value'"> </select> The View Model looks like this: var ViewModel = function() { this.titles = ko.observableArray([]); }; On DOM