Open bludev opened 6 years ago
Thanks @bluedev. I believe that mapping functionality will be subsumed by ko.proxy
for tko, but it requires ES6; I haven't yet given much thought to incorporating something like ko.mapping for ES5 browsers.
I haven't actually used the mapping plugin but seems a bit messy to map only the fields you want to observables in nested data structures. It seems like it would be easier to template out what the objects should look like.
Something like:
var item = ko.init({
name: null;
amount: ko.observable();
});
var options = {
makeSelfObservable: false,
map:{items:item}
}
var batch = ko.init(function(){
{
id: 0,
name: ko.observable(),
items:[{new item()}],
total: pureComputed(x => /*sum of item amounts*/)
}, options);
var x = new Batch();
Would set x to {
id: 0,
name: ko.observable(),
items:[{name:null, amount:ko.observable()}],
total: pureComputed(x => /*sum of items*/)
}
x.name("myName");
x.items[1].amount(5)
then passing in new Batch(ko.toJS(x)) would result in same structure (changed properties):
{
id: 0,
name: ko.observable("myName"),
items:[{name:null, amount:ko.observable(5)}],
total: pureComputed(x => /*sum of items*/)
}
It would probably also need a merge function with this, needing keys on the array to merge the same items.
Any thoughts from others or headaches that you experience with making certain properties observable in the model?
It seems like this would also promote putting functions relevant to sub objects in the actual object instead of most actions residing in the root view model.
It looks like a nice thing about the current mapping plugin is that logic can be added when a property is being mapped.
I am willing to work on this if people like the idea.
Thanks @abram27 . Incidentally, what're other observable libraries e.g. mobx doing for this sort of issue?
Looks like decorators are used for changing specific properties into observables in mobx: https://mobx.js.org/refguide/modifiers.html
Is there any intention to bring the mapping plugin into tko? http://knockoutjs.com/documentation/plugins-mapping.html As far as I am concerned, it is essential....