GithubHelp home page GithubHelp logo

What about a mapping plugin? about tko HOT 4 OPEN

knockout avatar knockout commented on July 19, 2024
What about a mapping plugin?

from tko.

Comments (4)

brianmhunt avatar brianmhunt commented on July 19, 2024 1

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.

from tko.

abram27 avatar abram27 commented on July 19, 2024

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.

from tko.

brianmhunt avatar brianmhunt commented on July 19, 2024

Thanks @abram27 . Incidentally, what're other observable libraries e.g. mobx doing for this sort of issue?

from tko.

abram27 avatar abram27 commented on July 19, 2024

Looks like decorators are used for changing specific properties into observables in mobx:
https://mobx.js.org/refguide/modifiers.html

from tko.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.