【问题标题】:Model to write jquery plugins编写jquery插件的模型
【发布时间】:2011-11-11 14:19:02
【问题描述】:

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

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

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

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

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

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

结果

Here你可以根据我阅读的所有信息看到我的插件模板。

【问题讨论】:

  • docs.jquery.com/Plugins/Authoring 有一些很好的指导方针
  • 您是在编写自己的库还是尝试扩展 jquery 核心库?
  • 我会避免使用 jQuery 插件作为架构模式。这是一个非常糟糕的模式。请改用模块加载器和模块化代码
  • @Incognito 我正在尝试创建一个模型来创建扩展 jquery 核心的插件。
  • @Raynos 你能说得更具体点或者给我一些例子吗?

标签: jquery plugins jquery-plugins modeling


【解决方案1】:

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

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

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

还有一个来自 ben alman 的关于编写插件的链接。

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

【讨论】:

  • 基于所有这些链接,我构建了自己的模板。当我完成测试后,我会发布一个链接。
【解决方案2】:

我通常使用类似这样的结构

(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''bar''baz' 事件的快捷方式。

【讨论】:

  • 请注意,所有这些部分都是可选的。没有任何 document.ready 或静态代码是完全有效的
  • 这并没有实现我要求的所有事情。即调用插件后如何获取或设置选项?
  • @Diego,乞丐不能成为选择者:-p 如何处理参数取决于您。您需要彻底了解您计划支持的每个不同的方法签名,并且需要进行类型检查以查看是否提供了正确的参数。
  • @Diego,我强烈建议您查看jQuery-UI widget factory。如果您要制作更大的小部件,它可以显着简化代码。
【解决方案3】:

我已经使用以下模板很长时间了,它似乎可以完成所需的一切并提供传统的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多