编写jquery插件的模型

发布于 2024-12-15 00:01:40 字数 589 浏览 0 评论 0原文

我必须创建一个文档,为如何为大型站点编写 jQuery 插件提供“模型”。

例如:所有插件都应该具有:

$.fn.somePlugin = function() {
    return this.each(function() {
        // Here the plugins does its goal.
    });
};

所以它们尊重流畅模型,并且可以同时使用多个元素调用。我认为它们都应该具有的其他一些东西是:

  • 选项 getter 和 setter(如 jQuery-ui 中)
  • 方法(如 jQuery-ui 中)
  • 更改默认选项的某种方法。当然,这应该在不修改插件文件的情况下完成(同样,像 jQuery-ui 一样)。

你的“模型插件”会怎样? (以尽可能最好的方式实现这一点以及您认为必要的其他一些事情)。

结果

在这里你可以看到我基于所有信息的插件模板我读了。

I have to create a document that provides a "model" for how to write jQuery plugins for a large site.

For example: all plugins should have:

$.fn.somePlugin = function() {
    return this.each(function() {
        // Here the plugins does its goal.
    });
};

so they respect the fluent model and also they can be called with multiple elements at same time. Some other things I think they should all have are:

  • Options getter and setter (like in jQuery-ui)
  • Methods (like in jQuery-ui)
  • Some way to change default options. Of course this should be done without modifying the plugin file (again, like jQuery-ui).

How would it be your "Model Plugin"? (achieving this and some other things you think necessary in the best possible way).

Result

Here you can see my plugin template based on all the information I read.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-12-22 00:01:40

jquery 文档有一个关于插件创作的部分:
http://docs.jquery.com/Plugins/Authoring

这是本的“幻灯片” almans 在波士顿 jquery 会议上谈论插件创作:

https://github.com/cowboy/talks/blob/master/jquery-plugin-authoring.js

以及 ben alman 提供的有关编写插件的另一个链接。

http://msdn.microsoft.com/en-us/scriptjunkie/ff696759

The jquery docs have a section on plugin authoring:
http://docs.jquery.com/Plugins/Authoring

and here's the "slides" from ben almans talk on plugin authoring from the boston jquery conference:

https://github.com/cowboy/talks/blob/master/jquery-plugin-authoring.js

and one more link from ben alman about writing plugins.

http://msdn.microsoft.com/en-us/scriptjunkie/ff696759

挽清梦 2024-12-22 00:01:40

我通常使用与此类似的结构,

(function ($, plugin) {
  "use strict";

  $[plugin] = function (options/* params */) {
    var settings;
    settings = $.extend({}, $[plugin].defaultSettings, options);
    //static funciton code here
  };

  $.fn[plugin] = function (options/* params */) {
    this.each(function (index, element) {
      var settings, $this;
      settings = $.extend({}, $.fn[plugin].defaultSettings, options);
      $this = $(this);
      $this.data(plugin+'Settings', settings);
      //chainable function code here
    });
    return this;
  };

  $[plugin].defaultSettings = {
    'foo': 'bar'
  };

  $.fn[plugin].defaultSettings = {
    'fizz': 'buzz'
  };

  $(function(){
    //document.ready initialization code here
  });
}(jQuery, 'foo'));

我通常不会打扰 plugin 参数,但它对于概括插件的名称可能很有用

对于事件快捷方式,我将使用

$.each('foo bar baz'.split(' '), function(i, name) {
  $.fn[name] = function(data,fn){
    if (fn == null) {
      fn = data;
      data = null;
    }
    return arguments.length > 0 ?
      this.bind(name, data, fn) :
      this.trigger(name);
  };
});

.foo().bar().baz() 都是绑定/触发 'foo'< 的快捷方式/code>, '酒吧',和 'baz' 事件。

I typically use a structure similar to this

(function ($, plugin) {
  "use strict";

  $[plugin] = function (options/* params */) {
    var settings;
    settings = $.extend({}, $[plugin].defaultSettings, options);
    //static funciton code here
  };

  $.fn[plugin] = function (options/* params */) {
    this.each(function (index, element) {
      var settings, $this;
      settings = $.extend({}, $.fn[plugin].defaultSettings, options);
      $this = $(this);
      $this.data(plugin+'Settings', settings);
      //chainable function code here
    });
    return this;
  };

  $[plugin].defaultSettings = {
    'foo': 'bar'
  };

  $.fn[plugin].defaultSettings = {
    'fizz': 'buzz'
  };

  $(function(){
    //document.ready initialization code here
  });
}(jQuery, 'foo'));

I don't usually bother with the plugin parameter, but it could be useful for generalizing the name of a plugin

For event shortcuts, I'll use:

$.each('foo bar baz'.split(' '), function(i, name) {
  $.fn[name] = function(data,fn){
    if (fn == null) {
      fn = data;
      data = null;
    }
    return arguments.length > 0 ?
      this.bind(name, data, fn) :
      this.trigger(name);
  };
});

Which will produce .foo(), .bar(), .baz() all as shortcuts for binding/triggering the 'foo', 'bar', and 'baz' events.

过去的过去 2024-12-22 00:01:40

我已经使用下面的模板很长时间了,它似乎可以完成所需的一切,并提供传统的 jQuery 脚本,例如: $.myPlugin("element", {options}) , $.myPlugin({options},callback), 或 '$("element").myPlugin();

(function($) {
    if (!$.myExample) { // check your plugin namespace does not already exist
        $.extend({  //  this will allow you to add your plugin to the jQuery lib
            myExample: function(elm, command, args) {
                //  keep in mind, right here you might want to do a class or data check to determine which direction this call is going
                //  for example, upon init the plugin on an element you may add the plugin name as a class, 
                //      this way, when it's recalled, you can see it alrady has that class and might be calling a command,
                //          thus make an if statemnt to push the process through
                return elm.each(function(index){
                    // do work to each element as its passed through
                    // be sure to use something like
                    //    return elm.each(function(e) { dor work });
                    // as your final statement in order to maintain "chainability"
                });
            }
        });
        $.fn.extend({   //  this gives the chainability functionality seen with $ funcs like: $("#eleID").css("color", "red") <--returns original element object
            myExample: function(command) {
                return $.myExample($(this), command, Array.prototype.slice.call(arguments, 1));
            }
        });
        $.myExample.props = {   //  Here you can establish specific properties to your plugin, prehaps even make them "Over-writable"
            key1: "value",
            key2: "value"
        };
        $.myExample.methods = { //  Here you can establish specific methods/functions for your plguin to carry out and maintain your namespace as well
            key1: function(param) {
                /*  do work */
            },
            key2: function(param) {
                /*  do work */
            }
        };
        //  This next part is not seen in many plugins but useful depending on what you're creating
        $.myExample.init = function(param) {    //  If you have an initialize method to apply, namespace it in here and calll on initializing your plugin
            var key = "value",
                key2 = {
                    subKey: "value"
                };
                /*
                /  run any number of initializing functions here
                /  I prefer to make my param a value that can be a
                /   string with a possible object
                /   the string for holding a base configuration
                /   the object for any change in properties or base values for that config
                */
        };
        $.myExample.defaults = {    //  establish base properties here that can be over-written via .props, but their values should never truly change
            key1: "value",
            key2: {
                prop1: {
                    subKey1: "value",
                    subKey2: "value"
                },
                prop2: {
                    subKey1: "value"
                }
            },
            key3: function(param) {

            }
        };
    }
})(jQuery);

I've been using the following template for a LONG time now and it seems to do everything needed as well as provide for traditional jQuery scripting, such as: $.myPlugin("element", {options}), $.myPlugin({options}, callback), or '$("element").myPlugin();

(function($) {
    if (!$.myExample) { // check your plugin namespace does not already exist
        $.extend({  //  this will allow you to add your plugin to the jQuery lib
            myExample: function(elm, command, args) {
                //  keep in mind, right here you might want to do a class or data check to determine which direction this call is going
                //  for example, upon init the plugin on an element you may add the plugin name as a class, 
                //      this way, when it's recalled, you can see it alrady has that class and might be calling a command,
                //          thus make an if statemnt to push the process through
                return elm.each(function(index){
                    // do work to each element as its passed through
                    // be sure to use something like
                    //    return elm.each(function(e) { dor work });
                    // as your final statement in order to maintain "chainability"
                });
            }
        });
        $.fn.extend({   //  this gives the chainability functionality seen with $ funcs like: $("#eleID").css("color", "red") <--returns original element object
            myExample: function(command) {
                return $.myExample($(this), command, Array.prototype.slice.call(arguments, 1));
            }
        });
        $.myExample.props = {   //  Here you can establish specific properties to your plugin, prehaps even make them "Over-writable"
            key1: "value",
            key2: "value"
        };
        $.myExample.methods = { //  Here you can establish specific methods/functions for your plguin to carry out and maintain your namespace as well
            key1: function(param) {
                /*  do work */
            },
            key2: function(param) {
                /*  do work */
            }
        };
        //  This next part is not seen in many plugins but useful depending on what you're creating
        $.myExample.init = function(param) {    //  If you have an initialize method to apply, namespace it in here and calll on initializing your plugin
            var key = "value",
                key2 = {
                    subKey: "value"
                };
                /*
                /  run any number of initializing functions here
                /  I prefer to make my param a value that can be a
                /   string with a possible object
                /   the string for holding a base configuration
                /   the object for any change in properties or base values for that config
                */
        };
        $.myExample.defaults = {    //  establish base properties here that can be over-written via .props, but their values should never truly change
            key1: "value",
            key2: {
                prop1: {
                    subKey1: "value",
                    subKey2: "value"
                },
                prop2: {
                    subKey1: "value"
                }
            },
            key3: function(param) {

            }
        };
    }
})(jQuery);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文