Javascript 将字符串解析为数据属性中的对象
我对jQuery验证插件有很多问题,解决这个问题的唯一方法是使用Javascript 将字符串解析为数据属性中的对象,javascript,object,Javascript,Object,我对jQuery验证插件有很多问题,解决这个问题的唯一方法是使用.submitHandler属性并在其内部执行一些技巧 其中之一是检查触发器的父级是否为字段集,如果它具有数据提交处理程序属性,它将执行我发送到那里的任何内容 它看起来像这样: <fieldset data-submit-handler="SITE.Products.QA.Bindings.post"> 在这种情况下,SITE.Products.QA.Bindings.post将是一个函数。问题是,我不知道如何将数
.submitHandler
属性并在其内部执行一些技巧
其中之一是检查触发器的父级是否为字段集,如果它具有数据提交处理程序
属性,它将执行我发送到那里的任何内容
它看起来像这样:
<fieldset data-submit-handler="SITE.Products.QA.Bindings.post">
在这种情况下,
SITE.Products.QA.Bindings.post将是一个函数。问题是,我不知道如何将数据属性中的字符串作为对象而不是字符串进行解析,以便执行引用的函数。有办法吗?您可以使用帮助器解决此问题:
// 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);
};
您可以使用帮助器来解决此问题:
// 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);
};
我没有相同的问题,我没有意识到上述问题所描述的对象。我只能通过发送字符串,所以你根本没有对象的引用<代码>站点
不是一个变量?在处理提交处理程序时,不,它在我的所有代码之前,它是插件操作的一部分。那么我想你必须提供更多关于上下文的信息。我无法想象如果无法访问对象,您将如何调用方法。想象一下,当调用该方法的处理程序时,SITE
属性还不可用,因此在解析该字符串作为引用时,我无法将其发送。我希望我更清楚,但我猜窗口
可能是我的参考?我没有相同的问题,我不知道上面问题描述的对象。我只能通过发送字符串,所以你根本没有对象的引用<代码>站点不是一个变量?在处理提交处理程序时,不,它在我的所有代码之前,它是插件操作的一部分。那么我想你必须提供更多关于上下文的信息。我无法想象如果无法访问对象,您将如何调用方法。想象一下,当调用该方法的处理程序时,SITE
属性还不可用,因此在解析该字符串作为引用时,我无法将其发送。我希望我更清楚,但我想窗口
可能是我的参考?