将字符串解析为来自数据属性的对象 [英] parse a string as an object from data attribute

查看:55
本文介绍了将字符串解析为来自数据属性的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 jQuery 验证插件时遇到了很多麻烦,我可以解决它的唯一方法是使用 .submitHandler 属性并在其中执行一些技巧.

I have a lot of troubles with jQuery validate plugin and the only way I could work around it it was by using the .submitHandler property and do some tricks inside it.

其中正在检查触发器的父级是否是字段集,如果它具有 data-submit-handler 属性,它将执行我在那里发送的任何内容.

On of which is checking if the parent of the trigger it's a fieldset and if it has an data-submit-handler attribute it will execute whatever I send there.

它看起来像这样:

<fieldset data-submit-handler="SITE.Products.QA.Bindings.post">

在这种情况下,SITE.Products.QA.Bindings.post 将是一个函数.问题是我不知道如何将 data 属性中的字符串解析为对象而不是字符串,以便我可以执行我引用的函数.有办法吗?

In which case the SITE.Products.QA.Bindings.post would be an function. The issue is that I have no clue how to parse that string from the data attribute as an object and not as a string so I can execute the function that I reference to. Is there a way to do it ?

推荐答案

您可以使用帮助程序来解决它:

You can use a helper that will resolve it:

// convert string representation of object in to object reference:
// @p: object path
// @r: root to begin with (default is window)
// if the object doesn't exist (e.g. a property isn't found along the way)
// the constant `undefined` is returned.
function getObj(p, r){
    var o = r || window;
    if (!p) return o;                      // short-circuit for empty parameter
    if(typeof p!=='string') p=p.toString();// must be string
    if(p.length==0) return o;              // must have something to parse
    var ps = p.replace(/\[(\w+)\]/g,'.$1') // handle arrays
              .replace(/^\./,'')           // remove empty elements
              .split('.');                 // get traverse list
    for (var i = 0; i < ps.length; i++){
        if (!(ps[i] in o)) return undefined; // short-circuit for bad key
        o = o[ps[i]];
    }
    return o;
}

// couple extensions to strings/objects

// Turns the string in to an object based on the path
// @this: the string you're calling the method from
// @r: (optional) root object to start traversing from
String.prototype.toObject = function(r){
    return getObj(this,r);
};

// Retrieves the supplied path base starting at the current object
// @this: the root object to start traversing from
// @s: the path to retrieve
Object.prototype.fromString = function(p){
    return getObj(p,this);
};

所以,示例用法:

window.foo = {
    bar: {
        baz: {
            hello: 'hello',
            world: function(){
                alert('world!');
            }
        }
    },
    list: [
        {item: 'a'},
        {item: 'b'},
        {item: 'c'}
    ]
};

var a = 'foo.bar.baz.hello';
console.log(getObj(a));    // hello
console.log(a.toObject()); // hello

var b = 'foo.bar.baz.world';
console.log(getObj(b)());  // world

var c = 'baz.hello';
console.log(getObj(c, foo.bar)); // hello
console.log(foo.bar.fromString(c)); // hello
console.log(c.toObject(foo.bar)); // hello

var d = 'foo.list[1].item';
console.log(getObj(d)); // b

这篇关于将字符串解析为来自数据属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆