knockout-mapping-plugin

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

Knockoutjs track changes after ajax call

孤街醉人 提交于 2020-01-07 01:52:33
问题 See JsFiddle here http://jsfiddle.net/WtgbV/2/ In words: I have some ajax call, and in the server's response I get some array of items (Items in knockout viewmodel) I need to know that property name was changed in element with id==2 etc to save changes automatically on server (via POST request) What is the simplest/easiest way to track changes in each element in Items array? 回答1: I co-wrote a component called DirtyFlag that detects changes in Knockout observables (or a set of them). You can

Knockout mapping complex object

烈酒焚心 提交于 2020-01-06 15:16:34
问题 For example, we have complex object: var complex = {a: 1, b: 2, c: {c1: 1, c2: 2}}; We want it to make observable: var observableComplex = ko.mapping.fromJS(complex); Question: why we get c variable not observable? I was seen somewher in manuals, that this is done by design and I want to know why? a - observable, b - observable c - object: c1 - observable c2 - observable 回答1: There is no object for 'c' that can be mapped...if you want it to be observed, you need to create a custom mapping for

Knockout mapping complex object

帅比萌擦擦* 提交于 2020-01-06 15:15:16
问题 For example, we have complex object: var complex = {a: 1, b: 2, c: {c1: 1, c2: 2}}; We want it to make observable: var observableComplex = ko.mapping.fromJS(complex); Question: why we get c variable not observable? I was seen somewher in manuals, that this is done by design and I want to know why? a - observable, b - observable c - object: c1 - observable c2 - observable 回答1: There is no object for 'c' that can be mapped...if you want it to be observed, you need to create a custom mapping for

Mapping a nested object as an observable from a complex JSON using the create callback

♀尐吖头ヾ 提交于 2020-01-06 11:45:10
问题 I've got a complex object in a JSON format. I'm using Knockout Mapping, customizing the create callback, and trying to make sure that every object that should be an observable - would actually be mapped as such. The following code is an example of what I've got: It enables the user to add cartItems , save them (as a JSON), empty the cart, and then load the saved items. The loading part fails: It doesn't display the loaded option (i.e., the loaded cartItemName ). I guess it's related to some

Mapping a nested object as an observable from a complex JSON using the create callback

跟風遠走 提交于 2020-01-06 11:45:06
问题 I've got a complex object in a JSON format. I'm using Knockout Mapping, customizing the create callback, and trying to make sure that every object that should be an observable - would actually be mapped as such. The following code is an example of what I've got: It enables the user to add cartItems , save them (as a JSON), empty the cart, and then load the saved items. The loading part fails: It doesn't display the loaded option (i.e., the loaded cartItemName ). I guess it's related to some

knockout.js partial mapping from json

﹥>﹥吖頭↗ 提交于 2020-01-06 08:27:12
问题 On the knockout.js site's documentation they say that when you get data back from the server you can do this: // Every time data is received from the server: ko.mapping.fromJS(data, viewModel); I'd like to partially map the data back into my object model. Is that possible? I have a viewModel.jobs[i].JobType child object, so I'd like to do something like this: ko.mapping.fromJS(data.jobType, viewModel.jobs[i].JobType); ... meaning I'd like to just map in the jobType from the result from the

Detect if a change occur in observableArray

狂风中的少年 提交于 2020-01-05 07:11:09
问题 Does anyone have idea, if it's possible to detect whenever a change occur inside observableArray while using ko mapping, this mean without having to create the model by hand? self.items = ko.observableArray([]) var data = ko.mapping.fromJS(result) self.items.push(data); I would like to log any changes occurred in any property inside my array of objects. Thanks 回答1: If I'm not mistaken, you should be able to use subscribe on the observable to get that information. See the bottom of this page

Knockout mapping for nested json

家住魔仙堡 提交于 2020-01-05 05:57:14
问题 I was trying to create a json schema viewer and editor. I need to show the dynamically generated nested json as nested table and must be able to edit them or add new. for that I was trying to map a nested json to knockout observable and to show them as a nested table JSBin sample But I failed to map it can anyone help me map it correctly and help me arrange the child tables as next 'tr'. I'm not getting any error but the values are not displayed. 回答1: The code is absolutely not clean, but it

Knockout validation after Mapping

两盒软妹~` 提交于 2020-01-05 04:18:12
问题 I'm new to knockout so please bear with me. I'm trying to using mapping plugin to map the Json data received from server to an existing viewModel instance. I'm able to do this without any issue. But in my viewModel I have used validation plugin so as soon as I map data and bind it to the UI, validation kicks and it immediately displays the error info. Is there way to not to display that error message until submit button is clicked. Or am I doing something wrong? Here is the Jsfiddle link For