knockout-2.0

Subscribe to observable array for new or removed entry only

允我心安 提交于 2019-11-28 06:07:45
So yes I can subscribe to an observable array: vm.myArray = ko.observableArray(); vm.myArray.subscribe(function(newVal){...}); The problem is the newVal passed to the function is the entire array. Is there anyway I can get only the delta part? Say the added or removed element? As of KnockoutJS 3.0, there's an arrayChange subscription option on ko.observableArray. var myArray = ko.observableArray(["Alpha", "Beta", "Gamma"]); myArray.subscribe(function(changes) { // For this example, we'll just print out the change info console.log(changes); }, null, "arrayChange"); myArray.push("newitem!"); In

Using Underscore Template with Knockout using interpolate due to asp.net

我与影子孤独终老i 提交于 2019-11-28 05:47:33
问题 Issue I need to use the underscore template instead of the default KnockoutJS template engine due to performance. However, since I'm in an asp.net environment the default tags of <% and %> will not work because of the asp.net handler. Working jsFiddle Not Working jsFiddle What I need is to apply something like the following: _.templateSettings = { interpolate : /\{\{(.+?)\}\}/g }; Making it use the {{ and }} tags Note 7: Using the Underscore.js template engine The Underscore.js template

Get previous value of an observable in subscribe of same observable

半腔热情 提交于 2019-11-28 03:38:06
Is it possible in knockout to get the current value of an observable within a subscription to that observable, before it receives the new value? Example: this.myObservable = ko.observable(); this.myObservable.subscribe(function(newValue){ //I'd like to get the previous value of 'myObservable' here before it's set to newValue }); There is a way to do a subscription to the before value like this: this.myObservable = ko.observable(); this.myObservable.subscribe(function(previousValue){ //I'd like to get the previous value of 'myObservable' here before it's set to newValue }, this, "beforeChange")

Knockout is not evaluating an expression when using $index in a binding

故事扮演 提交于 2019-11-28 00:47:17
Why is it, that when I try to use knockout.js to bind some text using $index, I get the code of a function instead of a number? <tbody data-bind="foreach: MyList"> <tr> <td><span data-bind="text: $index + 1"></span></td> </tr> </tbody> Instead of getting 1, 2, 3 etc., I get this: You can see, by the last character in the above image, that my index of zero is being added to 1. If I remove the '+ 1' from my binding, I get 0, 1, 2 instead of the function. How do I tell knockout to evaluate the expression? I have the same issue when I submit the form. My string fields are being submitted as a

Get dynamically inserted HTML to work with knockoutjs

我只是一个虾纸丫 提交于 2019-11-27 19:16:24
I'm using JQuery DataTables for all my tables because of all the nice built-in features, but it seems the only way to customize the table layout is to set the "sDom" option attribute for the DataTable and use something like $("div.SOMECLASS").html(HTML_HERE) to insert the customized html into the table. (FYI, i'm just trying to customize the header). The problem is I want the inserted html to use knockoutjs binding. Knockout doesn't seem to initialize the binding this way. Is there a way to work around this? This is part of the html that I want to insert. It's pretty much a drop down list of

Knockout.js Make every nested object an Observable

守給你的承諾、 提交于 2019-11-27 18:31:08
I am using Knockout.js as a MVVM library to bind my data to some pages. I'm currently building a library to make REST calls to a web service. My RESTful web service returns a simple structure: { id : 1, details: { name: "Johnny", surname: "Boy" } } I have an observable main parent, myObject . When I do myObject(ko.mapping.fromJS(data)) the observables in myObject are: id name surname How can I make details (and theoretically any object in the structure an observable)? I need this behavior so that i can set a computed observable on details and get noticed as soon as any of the internal data

Knockout: computed observable vs function

两盒软妹~` 提交于 2019-11-27 17:57:26
When using knockout, what is the advantage of using read-only computed observables rather than simple functions? Take the following viewmodel constructor and html snippet, for example: ​ var ViewModel = function(){ var self = this; self.someProperty = ko.observable("abc"); self.anotherProperty = ko.observable("xyz"); self.someComputedProperty = function(){ return self.someProperty() + self.anotherProperty(); }; }; <input data-bind="value: someProperty"/> <input data-bind="value: anotherProperty"/> <p data-bind="text: someComputedProperty()"></p> Everything here seems to work as you'd expect,

Is there a reason I would use Knockout MVC instead of Knockout JS?

社会主义新天地 提交于 2019-11-27 17:48:12
Another user suggested Knockout MVC to handle some AJAX posting issues. I read a little on it and I see it is a wrapper around Knockout JS . So I wonder what are the real differences between the two? Should I bother with Knockout JS since Knockout MVC exists? When would I use one over the other? Knockout MVC is the bastard child of WebForms . It routes all viewmodel methods through controller actions, meaning everything that happens has to bounce to the server and back. I cannot understand why anyone would take a framework like knockout, which is intended to be CLIENT SIDE MVVM, and force it

Radio buttons Knockoutjs

孤街醉人 提交于 2019-11-27 15:43:09
问题 I have 2 values that I get from server A and B. I can only have one true at a time. Again what I need is one of the radios to be checked at a time so one true value only. var viewModel = { radioSelectedOptionValue: ko.observable("false"), A: ko.observable("false"), B: ko.observable("false") }; ko.applyBindings(viewModel); <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/2.1.0/knockout-min.js"></script> <div class='liveExample'> <h3>Radio View model</h3> <table> <tr> <td class=

Update Knockout.js Observable from JSON

你离开我真会死。 提交于 2019-11-27 14:09:07
问题 I'm attempt to establish a grid and update it with more records via JSON. In this simple example I am able to achieve the required functionality but I can only update / push one JSON record. I would like to be able to add multiple records via JSON? How could I achieve this? I assumed I might have to create some sort of for loop and push each JSON result to the observable but I was hoping that knockout might have a better way of updating / parsing via JSON? Heres a copy of what I have achieved