knockout-mapping-plugin

Knockout's mapping breaks observable arrays

断了今生、忘了曾经 提交于 2019-12-05 13:15:22
I'm experiencing a strange problem with Knockout's mapping plug-in. If I fill an observable array through mapping I can't iterate the array or get its length, even though the UI is updated correctly, the array seems empty. You can find a working jsFiddle here: http://jsfiddle.net/VsbsC/ This is the HTML mark-up: <p><input data-bind="click: load" type="button" value="Load" /></p> <p><input data-bind="click: check" type="button" value="Check" /></p> <table> <tbody data-bind="foreach: items"> <tr> <td data-bind="text: name"></td> <td data-bind="text: value"></td> </tr> </tbody> </table> <p data

How to exclude certain properties from Knockoutjs toJS()

自作多情 提交于 2019-12-05 11:11:05
问题 I have the following model: var model = { A: 'One', B: 'Two', C: 'Three' }; I bind various UI elements to these fields, which works great. However, I convert the model back to a JavaScript object so I can save any changes to the server: var goingToServer = ko.toJS(model); goingToServer will include properties A, B and C. However, let's say property C is a huge chunk of data that will never change. I'd like to avoid sending this back to the server. Is there a way to make toJS() only include a

Signalr deserializes my objects incorrectly in IIS 7.5 and Edge/IE, foreverFrame broken?

落花浮王杯 提交于 2019-12-05 07:51:00
So I have a simple Signalr/Knockout project that uses the mapping plugin to bind a simple object (item with an array of more items) to viewModels I defined in JS: var someObjectMapping = { 'MyItemArray': { create: function (options) { return new MyItemViewModel(options.data); } } } var myItemMapping = { 'ItemChildren': { create: function (options) { return new ItemChildViewModel(options.data); } } } var SomeObjectViewModel = function (data) { ko.mapping.fromJS(data, someObjectMapping, this); } var MyItemViewModel = function (data) { ko.mapping.fromJS(data, myItemMapping, this); } var

Are computed observables on prototypes feasible in KnockoutJS?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 05:57:39
I have an array of items coming back from the service. I'm trying to define a computed observable for every Item instance, so my instinct tells me to put it on the prototype. One case for the computed observable: the system calculates points, but the user can choose to override the calculated value. I need to keep the calculated value available in case the user removes the override. I also need to coalesce user-assigned and calculated points, and add up the totals. I'm using mapping to do the following: var itemsViewModel; var items = [ { 'PointsCalculated' : 5.1 }, { 'PointsCalculated' : 2.37

KO Mapping issue with child objects

自闭症网瘾萝莉.ら 提交于 2019-12-05 03:30:41
I get the following data from the server: var data = [{ id: 0, child: { prop1 : 'a', prop2 : 'b' } } //Child object has data ,{ id: 0, child: null } ]; // Child object is null I'm having some issues after I map the data using the knockout mapping plugin. The issue is that the inner child object is not of the same type. After executing this: ko.mapping.fromJS(data, viewModel.data); I get that the first object has a child property of type Object with the data. However the second object has a property child of type Observable that when it's unwrapped returns null. How can I make that in both

Mapping: foreach binding work only the first time

痴心易碎 提交于 2019-12-04 19:32:12
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 to $.GetJSON . I get what I want, which is (browser output): Event1 Event2 Event3 But in subsequent

KnockOut Mapping Hierarchical JS object

心已入冬 提交于 2019-12-04 14:55:11
I am trying to create view Models using KnockOut mapping plugin , This is the object , Basically below is a sentence with words in it. var data = { name: 'Example Title', sentences: [ {id: 1, words: [{text: 'word1'}, {text: 'word2'}]}, {id: 2, words: [{text: 'word3'}, {text: 'word4'}]} ]}; I would like to have three view Models , Article should contain sentences , an Sentence should contain words var ArticleViewModel = function(data) { var self = this; self.id = ko.observable(data.id); self.sentences = ko.observableArray([]); } var SentenceViewModel = function(data) { var self = this; self.id

Knockout DateTime Picker - Default Date not Binding

删除回忆录丶 提交于 2019-12-04 13:21:10
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.registerEventHandler(element, "dp.change", function(event) { var value = valueAccessor(); if (ko.isObservable

Using Knockout mapping for complex JSON

风流意气都作罢 提交于 2019-12-04 13:04:49
Most of Knockout seems very intuitive. One thing that is strange to me though is how the mapping plugin works. I was expecting/hoping I would be able to feed it JSON from an ajax call, and have a sort of "dynamic" view model that is available to reference in my HTML. The description of the mapping plugin even makes it sound like this is how it works: "If your data structures become more complex (e.g. they contain children or contain arrays) this becomes very cumbersome to handle manually. What the mapping plugin allows you to do is create a mapping from the regular JavaScript object (or JSON

Performance tuning a knockout application - guidelines for improving response times

北城以北 提交于 2019-12-04 07:40:29
问题 I have a large, complex page that relies heavily on knockout.js. Performance is starting to become an issue but examining the call stack and trying to find the bottlenecks is a real challenge. I noticed in another question ( Knockout.js -- understanding foreach and with ) that the accepted answer has the comment: ...and I suggest not using with where high performance is necessary because of the overhead... Assuming the statement is true, this is really useful stuff to know and I have not