knockout.js

Knockout/Select2: Triggering select2 to update based on an observable option updating

烈酒焚心 提交于 2020-01-24 17:58:26
问题 When upgrading from Knockout.js 2.x to 3.x, I've noticed this not working: I have a screen where I have a <select> depending on an observable array of observables, and I wrap the <select> with a Select2 wrapper. Now, it used to be that when one of the option observables was updated, the select would update. And that is still the case. But I can't get the Select2 binding to update properly at the same time. I'm using the binding handlers recommended by the Select2 Github page: ko

Knockout: writing and reading a computed property

风流意气都作罢 提交于 2020-01-24 12:56:47
问题 I have a model with two property: title and content and what I want to do is: If title has a value, use it but in case it's blank use the first 20 chars + "..." from content. This is the model: function Note(title, content) { var self = this; self.content = ko.observable(content); self.title = ko.computed({ read: function(){ if(!title){ var content = self.content(); if(content) return content.substring(0,19) + "..."; } }, write: function(title){ return title; } }); } Title value gets

Knockoutjs nested observableArrays

ぃ、小莉子 提交于 2020-01-24 12:47:09
问题 I have some trouble with knockout nested observableArrays. My Data looks like this: var myData = { Id: 123, Name: "string here", Categories: [ { Id: 12, Name: "Category Name", Products: [ { Id: 1, Name: 'Product1 name', SomethingElse: '...' }, { Id: 2, Name: 'Product2 name', SomethingElse: '...' } ] },{ // another category ... } ] } My ViewModel looks like this: viewModel = function () { var self = this; this.Id = menueItem ? ko.observable(menueItem.Id) : ko.observable(0); this.Name =

ko.validation.group did not detect my errors on my nested observableArray

陌路散爱 提交于 2020-01-24 10:07:45
问题 I develop a project with Durandal/Breeze using intensive Knockout bindings. I have a view which is using the following observable: packing - description - weight - ... - isotopes - name - activity - ... As you can see above: my packing observable contains an isotopes observableArray inside. This packing object is filled from breeze with a query. var query = entityQuery.from('Packings') .where('id', '==', packingId) .expand('isotopes'); I try to set validation in place for all of this using ko

Knockout ViewModel computed garbage collection

烂漫一生 提交于 2020-01-24 04:56:11
问题 I've been trying to track down any issues with garbage collection within my application code. I've stripped it down to pure knockout code and there seems to be an issue collecting created objects depending on how a computed property is created. Please see the following JS fiddle: http://jsfiddle.net/SGXJG/ Open Chrome profiler. Take a heap snapshot Click Make All Take another heap snapshot Compare the snapshots Test2 & Test3 still remain in memory while Test1 is correctly collected. Please

Knockout.js validation using extenders - prevent validation on load

别等时光非礼了梦想. 提交于 2020-01-24 04:16:25
问题 I have implemented a very basic required validation on "first name" largely based on the example suggested on knockout website (http://knockoutjs.com/documentation/extenders.html) - Live example 2: Adding validation to an observable. My problem is that I don't want the validation to fire when the form is first loaded. Below is my code <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Test</title> <link href="Style.css" rel="stylesheet" /> </head> <body> <p data-bind=

Context of this when triggering method on root from child

久未见 提交于 2020-01-23 09:22:19
问题 I have this very simple test view <button data-bind="click: add">Add</button> <table data-bind="foreach: items"> <tr> <td data-bind="text: name"></td> <td><button data-bind="click: $root.remove">Remove</button></td> <tr> </table> Fiddle: http://jsfiddle.net/6PP5m/ The problem is that the context of this in the remove method is the child view model that fired the click event I havent found a solution that I like, you can add a self variable in the contructor and use that instead of "this", but

KnockoutJS fromJS not working TypeError: Cannot call method 'fromJS' of undefined

别说谁变了你拦得住时间么 提交于 2020-01-23 05:48:27
问题 I use knockoutJS and i had the following error when i use "fromJS" TypeError: Cannot call method 'fromJS' of undefined My JavaScript Code <script src="../../Scripts/jquery-1.8.3.js" type="text/javascript"></script> <script src="../../Scripts/knockout-2.1.0.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { var PersonModel = function (data) { ko.mapping.fromJS(data, {}, this); }; var data = $.getJSON("http://localhost:40913/candidate/index/1",

Mapping: foreach binding work only the first time

限于喜欢 提交于 2020-01-23 03:51:07
问题 I have the following snippet JQuery inside an HTML file: $.getJSON("/events/", function (data) { viewModel = ko.mapping.fromJS(data); ko.applyBindings(viewModel); }); The code is executed when, for example, the user presses a button and returns JSON like: {"Events":[{"Name":"Event1"},{"Name":"Event2"},{"Name":"Event3"}]} This result is linked (using KnockoutJS ) to: <ul data-bind="foreach: Events"> <li><span data-bind="text: Name"></span></li> </ul> Everything works fine with the first call

how to upload image and save it in database using knockout js?

与世无争的帅哥 提交于 2020-01-22 15:09:22
问题 hey m using mvc3 with knockout and trying to use knockout binding to upload and save uploaded image in database. i am able to browse and get the image but not getting as how to save that image. my html view is: <div data-bind="foreach: { data: images, beforeRemove: beforeRemoveSlot }"> <div> <input type="file" accept="image/*" data-bind="file: imageFile, fileObjectURL: imageObjectURL, fileBinaryData: imageBinary"/> <div data-bind="if: imageObjectURL"> <img class="thumb" data-bind="attr: { src