[英]How can I give my default array of properties access to the passed jQuery object in my plugin?
Say I have the following plugin, enclosed within an immediately invoked function expression: 说我有以下插件,包含在立即调用的函数表达式中:
$.fn.someFunc = function(tags, options) {
// Containing element
$self = $(this);
opts = $.extend({}, $.fn.someFunc.defaults, options);
// More code here
return $self;
}
$.fn.someFunc.defaults = {
property: { prop: value, prop2: value2 },
anotherProp : { prop: value, prop2: value2 }
}
How do I give my array $.fn.someFunc.defaults
access to my $self
variable? 如何为数组
$.fn.someFunc.defaults
访问$self
变量? Say I want to set the default value of $.fn.someFunc.defaults.property.prop
to something that is dependent on some intrinsic property of the element the plugin has been provided access to? 假设我想将
$.fn.someFunc.defaults.property.prop
的默认值设置为取决于插件已提供访问权限的元素的某些固有属性? Such as: 如:
$.fn.someFunc.defaults = {
property: { prop: $self.width(), prop2: value2 }, // This currently does not work
anotherProp : { prop: value, prop2: value2 }
}
Attempting to do so currently results in a ReferenceError: $self is not defined
error. 目前尝试这样做会导致
ReferenceError: $self is not defined
错误。 Any ideas? 有任何想法吗?
You cannot do it this way. 您无法以这种方式这样做。 Because
$self
is a variable local to someFunc
, you need to pass it to defaultoptions
. 因为
$self
是someFunc
局部变量,所以您需要将其传递给defaultoptions
。
Try this: 尝试这个:
Make your default option as function 将默认选项设置为功能
$.fn.someFunc.defaults = function(){
return { property: { prop: this.width(), prop2: value2 }, // This currently does not work
anotherProp : { prop: value, prop2: value2 }};
}
What you are NOT doing is exposing your $self
object by returning a reference to $(this)
object from your extension function 你不这样做是暴露你的
$self
通过返回参考对象$(this)
从你的扩展函数对象
Try adding return $self
尝试添加
return $self
$.fn.someFunc = function(tags, options) {
// Containing element
$self = $(this);
opts = $.extend({}, $.fn.someFunc.defaults, options);
return $self;
}
This is how plug-ins allow for chaining and access to manipulated data / elements. 这就是插件如何允许链接和访问操纵的数据/元素的方式。
I don't think you can. 我认为你不能。
$self
(and the this
that it's from) is only defined when the function is called which is sensible because it can vary depending what you call the function on. $self
(与this
,它来自的)当函数被调用这是明智的,因为它可以根据你所说的功能有所不同才被定义。
You're defining the defaults outside the function and so this
isn't known, it's also evaluated at this point. 你定义的默认功能之外,所以
this
是不知道,它也是在这一点上进行评估。 What you want to do is ideally only have the variable evaluated later during the function. 理想情况下,您只想在函数执行过程中对变量进行求值。 There are ways to do that but they're going to overly complicate things for someone coming along and trying to change the defaults later.
有很多方法可以做到,但是对于那些后来尝试更改默认值的人来说,它们会使事情变得过于复杂。
There are a couple of simple options, either define those defaults inside the function 有几个简单的选项,可以在函数中定义这些默认值
$.fn.someFunc = function(tags, options) {
// Containing element
$self = $(this);
derived_defaults = {
property: { prop: $self.width() }
};
opts = $.extend(true, derived_defaults, $.fn.someFunc.defaults, options);
// More code here
return $self;
}
$.fn.someFunc.defaults = {
property: { prop2: value2 }, // This currently does not work
anotherProp : { prop: value, prop2: value2 }
}
Someone can still override that default outside (you'll need to document that this is possible) but they can't do something that depends on accessing $self
. 有人仍然可以在外部覆盖该默认值(您需要证明这是可能的),但他们不能做依赖于访问
$self
事情。
Alternatively -- if the value is related to something concrete like width -- you could process it internally, so allow values such as '100px' for an absolute value and '90%' uses 90% of the width of $self
. 另外,如果该值与诸如width之类的具体东西有关,则可以在内部进行处理,因此允许诸如'100px'的值作为绝对值,而'90%'则使用
$self
宽度的90%。 You'd detect this inside the plugin and derive the correct value. 您将在插件中检测到此错误并得出正确的值。
Such information should not be stored in the options array. 此类信息不应存储在options数组中。 Options are meant for configuration.
选项用于配置。 You shouldn't need your DOM element to fill those default options.
您不需要DOM元素来填充这些默认选项。
Try this jQuery plugin boilerplate: 试试这个jQuery插件样板:
https://github.com/guidobouman/jquery-plugin-boilerplate https://github.com/guidobouman/jquery-plugin-boilerplate
A rather popular jQuery plugin is based on that boilerplate: 一个相当流行的jQuery插件基于该样板:
https://github.com/guidobouman/jquery-panelsnap https://github.com/guidobouman/jquery-panelsnap
That plugin picks up on the CSS styling and stores that value. 该插件采用CSS样式并存储该值。 This way a user can style his content, and the plugin picks up on that.
这样,用户就可以设置其内容的样式,然后插件就会开始处理。 Configuring the width of a plugin within javascript is bound to give issues.
在javascript中配置插件的宽度必然会引起问题。 Styling belongs within CSS.
样式属于CSS。
Javascript: Function, CSS: Style Javascript:函数,CSS:样式
Disclaimer: 免责声明:
The links provided point to code written by me. 提供的链接指向我编写的代码。
Edit: 编辑:
If you want to get specific data from your element I'd store that in a different property in your plugin. 如果您想从元素中获取特定数据,则可以将其存储在插件的其他属性中。 To get all
data-
based attributes from your DOM element use the following: 要从DOM元素获取所有基于
data-
的属性,请使用以下命令:
$.fn.someFunc = function(tags, options) {
// Containing element
$self = $(this);
opts = $.extend({}, $.fn.someFunc.defaults, options);
element_data = $self.data();
// More code here
return $self;
}
NB: I did not correct the linting errors in your code. 注意:我没有纠正代码中的掉毛错误。
You are returning the wrong object ( $(this)
). 您返回了错误的对象(
$(this)
)。 In other words you are returning a jquery object wrapped inside another jquery object. 换句话说,您将返回包装在另一个jquery对象中的jquery对象。 You should be returning
this
instead. 您应该返回
this
。
Make the following modification to the somFunc function:- 对somFunc函数进行以下修改:
$.fn.someFunc = function(tags, options) {
// Containing element
// $self = $(this);
// Note i have commented out the above line.
$self = this;
opts = $.extend({}, $.fn.someFunc.defaults, options);
// More code here
return $self;
}
I think that should fix the problem. 我认为那应该解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.