knockout-mapping-plugin

KnockoutJS proper way to update observableArray AJAX

。_饼干妹妹 提交于 2019-12-04 06:41:12
In KnockoutJS, what's the proper way to update an observableArray of JSON data each time an AJAX command is run? Right now, I'm blanking the array using something like viewmodel.items([]), then repopulating it with the JSON data from the server. Short of using the KnockoutJS mapping plugin (which might be the only way to do this) what is the correct path? My server logic is going to send some of the same data each time, so I can't just iterate and push the items into the array unless I want duplicates. //// Adding how I'm doing it today //// I'm not sure why I'm doing it this way, but this is

How to exclude certain properties from Knockoutjs toJS()

这一生的挚爱 提交于 2019-12-04 00:36:51
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 predefined set of fields when converting a model back to a JavaScript object? One thing I've been

Delete Item from List using Knockout.js

冷暖自知 提交于 2019-12-03 16:15:07
I am trying to delete an item from a list. I am using knockout.js with the mapping plugin. My code looks like this: Serialize to Json @{ var jsonData = new HtmlString(new JavaScriptSerializer().Serialize(Model));} Template <script type="text/html" id="imgsList"> {{each model.Imgs}} <div style="float:left; margin: 10px 10px 10px 0;"> <div><a href="${Filename}"><img src="${Filename}" style="width:100px;"></img></a></div> <div data-bind="click: deleteImage">Delete</div> </div> {{/each}} </script> K.O. JavaScript <script type="text/javascript"> $(function() { //KO Setup var viewModel = { "model":

Best way to get only modified rows from observableArray (when there is bulk edit option)

穿精又带淫゛_ 提交于 2019-12-03 15:46:25
I have an ObservableArray collection which binds to the HTML table with bulk edit option (MVC3), every time the user hits commit I wanted to send only the modified rows from the collection instead of sending entire viewmodel list, please advise if any best way to track or filter only the modified rows. Here is a post about creating a dirty flag in Knockout that will track changes to all observables in an object. Typically, you would add a dirty flag to each item in your array in a constructor function or loop through each item and add the flag. Then, you can create a computed observable to

KnockoutJS - Update ViewModel / Mapping Plugin

时间秒杀一切 提交于 2019-12-03 13:19:59
How can i update the complete viewModel ? On page load i get a Model and convert that using ko.mapping.fromJS(myObject) to a viewModel. If the user clicks a button i want to get updated data from the server Now i want to apply theese updates If i use ko.applyBindings(viewModel); it updates the ui perfectly. But it adds the same events again. So if the user clicks the button, the event gets fired twice, third and so on. Question What is a good way to update my complete viewModel. Maybe i remove the bindings and apply them again ? (how to do this). Sample var viewModel; function update() { $

ko.mapping create function, extend object

怎甘沉沦 提交于 2019-12-03 07:55:37
Is it possible to modify the (for lack of a better term) schema of an object during the mapping process? I would imagine it is, I just can't seem to get it to work. I'm trying something like this: var data = { itemOne: 'someData', itemTwo: 'moreData' } var mapping = { "newItem": { create: function(options) { return ko.observable(false); } } }; ko.mapping.fromJS(data, mapping, _model.observableArrayPart); Here is a sample that shows customizing how your array is creating and defining a key for it, so that you can apply updates using the mapping plugin: http://jsfiddle.net/rniemeyer/LHeQZ/ var

How do I make a deep copy of a knockout object that was created by the mapping plugin

孤人 提交于 2019-12-03 07:43:48
问题 Here's my scenario. I'm using the knockout mapping plugin to create an observable viewmodel hierarchy for me. My hierarchy has nested elements in it. At a particular point in the hierarchy I want to put an Add button to insert a new blank copy of that element in the observablearray. The problem is I can't just say whateverArray.push(new MyObject()). Since the mapping plugin actually created the whole hierarchy for me, I don't have access to "MyObject". So it seems the only thing I can do to

KnockoutJS subscribe to property changes with Mapping Plugin

时光总嘲笑我的痴心妄想 提交于 2019-12-03 05:40:24
问题 Is there anyway I can tell the knockout mapping plugin to subscribe to all property changes call a certain function? I realize I can manually subscribe to the property change event in this manner: var viewModel = { name: ko.observable('foo'), } // subscribe manually here viewModel.name.subscribe(function(newValue){ // do work }) I would like to be able to generically subscribe though, since my view models may vary, I don't want to hardcode the property names. I created a function that does

How do I make a deep copy of a knockout object that was created by the mapping plugin

烂漫一生 提交于 2019-12-02 20:28:49
Here's my scenario. I'm using the knockout mapping plugin to create an observable viewmodel hierarchy for me. My hierarchy has nested elements in it. At a particular point in the hierarchy I want to put an Add button to insert a new blank copy of that element in the observablearray. The problem is I can't just say whateverArray.push(new MyObject()). Since the mapping plugin actually created the whole hierarchy for me, I don't have access to "MyObject". So it seems the only thing I can do to insert a new item is to look at a previous item and copy it. I tried the ko.utils.extend function, but

KnockoutJS subscribe to property changes with Mapping Plugin

和自甴很熟 提交于 2019-12-02 20:15:57
Is there anyway I can tell the knockout mapping plugin to subscribe to all property changes call a certain function? I realize I can manually subscribe to the property change event in this manner: var viewModel = { name: ko.observable('foo'), } // subscribe manually here viewModel.name.subscribe(function(newValue){ // do work }) I would like to be able to generically subscribe though, since my view models may vary, I don't want to hardcode the property names. I created a function that does this, but it may not be the best approach. It works over all browsers except IE7 and below. Here I take a