简体   繁体   English

为什么函数不接收输入的JSON数据?

[英]Why function does not receive inputted JSON data?

I've got a custom jQuery plugin I'm working on with a bit of issue with JSON. 我有一个自定义的jQuery插件,正在处理JSON的一些问题。

I'm using the jQuery plugin boilerplate to create this and am setting the default properties at the top like so: 我正在使用jQuery插件模板来创建它,并在顶部设置默认属性,如下所示:

// Create the defaults once
var pluginName = "customHighlight",
        defaults = {
            actions: [{"test_action":"Test action"}],
            position: "left"
};

The issue is that when I try to set my own properties on initiation of the plugin like so: 问题是当我尝试在插件初始化时设置自己的属性时,如下所示:

$('div,p').customHighlight({
     actions: [{"1_action":"1 action"}]
});

I get nothing coming through. 我什么也没经过。 Am I making a school boy error that I haven't caught here? 我是在犯一个我没有在这里发现的男生错误吗?

It's not quite clear what you mean by "nothing coming through". 不清楚“什么都不会发生”是什么意思。 It's entirely possible that you're making a mistake in your implementation of the boilerplate, and you haven't shown any of the relevant code, so we can't point that out for you, but there is one general design flaw in your setup. 您很可能在实现样板的过程中犯了一个错误,并且没有显示任何相关代码,因此我们无法为您指出这一点,但是设置中存在一个常规设计缺陷。

If you're using the boilerplate correctly, your plugin's settings property will look like this: 如果正确使用样板,则插件的settings属性将如下所示:

{
    actions: [{"1_action":"1 action"}],
    position: "left"
}

I assume you were expecting actions to be an array with two objects. 我假设您期望actions是一个包含两个对象的数组。 This outcome is because you are specifying a value for actions , and so $.extend which is being used in Plugin.prototype.init doesn't use the default value. 结果是因为您正在为actions指定一个值,所以在Plugin.prototype.init中使用的$.extend不使用默认值。

You could solve this by changing this line in init : 您可以通过更改init这一行来解决此问题:

this.settings = $.extend( {}, defaults, options );

To this: 对此:

this.settings = $.extend( true, {}, defaults, options );
                      //  ^ "deep" param

Setting deep indicates that $.extend should be applied recursively. 设置为deep表示$.extend应该递归应用。 But this doesn't work for arrays, so you also need to make sure actions is an object: 但这不适用于数组,因此您还需要确保actions是一个对象:

var pluginName = "customHighlight",
        defaults = {
            actions: {"test_action":"Test action"},
            position: "left"
};

Now, calling customHighlight like this: 现在,这样调用customHighlight

$('div,p').customHighlight({
     actions: {"1_action":"1 action"}
});

Would result in a settings object that looks like this: 将产生一个如下所示的settings对象:

{
    actions: {
       "test_action": "Test action"
       "1_action": "1 action"
    },
    position: "left"
}

Fiddle 小提琴

I realised that the settings object is what I needed to retrieve, instead of the defaults object that I was retrieving in my code. 我意识到设置对象是我需要检索的,而不是我在代码中检索的默认对象。 I didn't provide enough info here for a successful answer to be given so apologies. 我在这里没有提供足够的信息,无法对这样的成功答案表示歉意。 But the issue is now resolved. 但是问题现在已经解决。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM