English 中文(简体)
有哪些 JavaScript 对象映射库的天体? [已关闭]
原标题:What JavaScript object to object mapping libraries exist? [closed]

我们不允许为软件库、辅导、工具、书籍或其他场外资源寻求建议的问题。 您可以编辑问题, 以便用事实和引用来解答问题。

Closed 2 years ago.

我正在研究一个大型的 JavaScript 项目, 我正忙于将 JasavaScript 数据( 从后端) 输入到我自己的 JavaScript 对象 。

我使用Knockout JavaScript MVVM 框架, 虽然包括一个 < a href="http://knockoutjs.com/ documentation/plexins-mapping.html" rel=“noreferr”>映射插件 , 但它不允许我实际重新映射属性。 我想做到这一点, 因为连接JSON的数据太精细, 我想平整我的JSU 对象。 一个例子如下 。

连接数据。

Object : {
    Description: {
        Id : 1,
        Title :  ProductX 
    },
    Price : {
        Last : 12,
        Currency : 3
    }
}

我想重新绘制/缩放此图,以便:

var mappedObject = {
    id : 1,
    title:  ProductX ,
    price : 12,
    currency : 3
}

因此,我想提供一个映射配置,详细描述哪些连接属性应映射到哪些外向属性 < / 坚固 > 。 非常像 < a href=" http://dozer. sourceforge.net/" rel=" noreferrer" > Dozer 正在配置。

我的问题是:“强者”有没有图书馆能够实现我所希望的“强者”目标? 还是这需要我建立自己的图书馆?

最佳回答

事实上, Knockoutjs 映射插件允许您仅此而已:

ko.mapping.from JS call 中的 ko.mapping.from JS call中,您可以提供一个映射对象,用于映射包含属性...

var mapper = {
    create: function(options){
        return { name: ko.observable(options.data.name) };
    }
};

这意味着使用此绘图插件的映像器, 每个天体将被平整为仅包含其名称为可观测对象的天体 。

你像这样使用它:

var viewModel = ko.mapping.fromJS({id: 1, name: "a", desc: "b"}, mapper);

在这种情况下, viewModel 将只有一个属性名称。

您可以在官方文件“http://knockoutjs.com/documentation/plugins-mapping.html' rel=“no follow'>这里中阅读更多关于这个特点的更多信息。

问题回答

自上次更新这个专题以来已有一段时间了。 由于人们现在可能还在搜索物体对物体的映像仪:

去年,我创建了一个C# Automapper 实施到 TypeScript / JavaScript / JavaScript 的端口,正好是为了这一设想。我已经在 GitHub (https://b.ldmn.nl/AutoMapperts ) 设置了代码。您也可以使用自动映射国家预防机制或鲍尔软件包直接使用图书馆。

图书馆几乎有完整的文件记录。 此外,很多茉莉花单位的测试(代码覆盖率约为95% ) 。 他们应该向您解释一下您需要什么。

我希望这个图书馆适合你的需要,如果你有任何问题和(或)意见,请不要迟疑地联系我!

嗯,我不认为有 任何图书馆 这一点,因为这听起来很容易做。

以下是一个例子:

var obj = {
    Description: {
        Id : 1,
        Title :  ProductX 
    },
    Price : {
        Last : 12,
        Currency : 3
    }
},
    mappedObject = {};

function mapThat( obj, mappedObject ) {
    Object.keys( obj ).forEach( function( key ) {
        if ( typeof obj[ key ] ===  object  ) {
            // If it s an object, let s go recursive
            mapThat( obj[ key ], mappedObject );
        }
        else {
            // If it s not, add a key/value
            mappedObject[ key.toLowerCase() ] = obj[ key ];
        }
    } );
}

mapThat( obj, mappedObject );

console.log( mappedObject );​

这里的 Demo:http://jsfiddle.net/qG6hm/

  1. you can use Object-mapper , this is very basic and simple library for object mapping in Javascript/JSON. when using this library, you need to specify the mapping property combinations and source and target types in a separate file.

  1. Also there is another library AutoMapper, this is slightly advanced. It can take care of mappings automatically based on property names, But you can still mention custom mapping for which ever it s not straight forward. If you re from .net background, this library implements in the same way as C# automapper.




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签