Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit c99821d

Browse files
author
Kahlil Lechelt
committed
Add sub-generator for jQuery plugins.
1 parent c1e8b4e commit c99821d

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

jqueryplugin/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
var util = require('util');
3+
var yeoman = require('yeoman-generator');
4+
5+
var JquerypluginGenerator = module.exports = function JquerypluginGenerator() {
6+
yeoman.generators.NamedBase.apply(this, arguments);
7+
};
8+
9+
util.inherits(JquerypluginGenerator, yeoman.generators.NamedBase);
10+
11+
JquerypluginGenerator.prototype.boilerplate = function boilerplate() {
12+
this.copy('jquery.plugin.js', 'js/plugins/jquery.' + this.name + '.js');
13+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*!
2+
* jQuery lightweight plugin boilerplate
3+
* Original author: @ajpiano
4+
* Further changes, comments: @addyosmani
5+
* Licensed under the MIT license
6+
*/
7+
8+
// the semi-colon before the function invocation is a safety
9+
// net against concatenated scripts and/or other plugins
10+
// that are not closed properly.
11+
;(function($, window, document, undefined) {
12+
13+
// undefined is used here as the undefined global
14+
// variable in ECMAScript 3 and is mutable (i.e. it can
15+
// be changed by someone else). undefined isn't really
16+
// being passed in so we can ensure that its value is
17+
// truly undefined. In ES5, undefined can no longer be
18+
// modified.
19+
20+
// window and document are passed through as local
21+
// variables rather than as globals, because this (slightly)
22+
// quickens the resolution process and can be more
23+
// efficiently minified (especially when both are
24+
// regularly referenced in your plugin).
25+
26+
// Create the defaults once
27+
var pluginName = 'defaultPluginName';
28+
var defaults = {
29+
propertyName: "value"
30+
};
31+
32+
// The actual plugin constructor
33+
function Plugin(element, options) {
34+
this.element = element;
35+
36+
// jQuery has an extend method that merges the
37+
// contents of two or more objects, storing the
38+
// result in the first object. The first object
39+
// is generally empty because we don't want to alter
40+
// the default options for future instances of the plugin
41+
this.options = $.extend({}, defaults, options);
42+
43+
this._defaults = defaults;
44+
this._name = pluginName;
45+
46+
this.init();
47+
}
48+
49+
Plugin.prototype = {
50+
51+
init: function init() {
52+
// Place initialization logic here
53+
// You already have access to the DOM element and
54+
// the options via the instance, e.g. this.element
55+
// and this.options
56+
// you can add more functions like the one below and
57+
// call them like so: this.otherFunction(this.element, this.options).
58+
},
59+
60+
otherFunction: function otherFunction(el, options) {
61+
// some logic
62+
}
63+
};
64+
65+
// A really lightweight plugin wrapper around the constructor,
66+
// preventing against multiple instantiations
67+
$.fn[pluginName] = function(options) {
68+
return this.each(function () {
69+
if (!$.data(this, 'plugin_' + pluginName)) {
70+
$.data(this, 'plugin_' + pluginName,
71+
new Plugin(this, options));
72+
}
73+
});
74+
}
75+
76+
})(jQuery, window, document);

0 commit comments

Comments
 (0)