jQuery Boilerplate能够将$ .myplugin添加到返回值或对象

时间:2014-12-03 03:05:35

标签: jquery jquery-plugins jquery-boilerplate

我正在从jQuery boilerplate创建一个jQuery插件。到目前为止,我在实施方面都取得了成功。

但是,我希望我的插件可以这样调用

$('selector').myplugin({data}) -- and have it return a chainable object

$.myplugin({data}) --and have it return something (string, object, array).

我读到了将this.each() ... return this移到每个方法,但我得到了

TypeError: undefined is not a function (evaluating '$.myplugin()')

实施例

以下是我想要做的一个例子。

<input class="textIn" type="text" />
<input class="textOut" type="text" />

<script>
  var input = $('.textIn').val();

  //CASE #1
  // will perform method named someMethod with an arg and return a value
  var output = $.myPlugin(someMethod, input);

  //CASE #2
  // will go through each node with class 'textOut' and perform someMethod with an arg
  $('.textOut').myPlugin(someMethod, input);

  //CASE #3
  // will go through each node with class 'textOut' and perform someOtherMethod
  $('.textOut').myPlugin(someOtherMethod);
</script>

也许情况#1和#2不能合并,我将不得不重命名该方法,但我不确定。任何帮助都会很棒!

0 个答案:

没有答案