Skip to content

Commit 4905c55

Browse files
committed
Merge pull request #23 from azu/jQuery-plguin
jQueryプラグインのサンプル実装
2 parents f4d7687 + d12105e commit 4905c55

File tree

9 files changed

+69
-32
lines changed

9 files changed

+69
-32
lines changed

Diff for: .eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"es6": true,
2222
"node": true,
2323
"browser": true,
24+
"jquery": true,
2425
"mocha": true
2526
},
2627
"ecmaFeatures": {

Diff for: ja/jQuery/README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
# jQueryのPlugin Pattern
1+
# jQueryのPlugin
22

3-
!CODEFILE "../../src/jQuery/jquery.js"
3+
jQueryでは`$.fn`を拡張する事で、`$()`の返り値であるjQueryオブジェクトにメソッドを追加することが出来ます。
44

5-
```js
6-
var jQuery = require("jquery");
7-
jQuery(document.body);
8-
```
5+
次の`greenify`プラグインでは、`$(document.body).greenify();`というメソッド呼び出しが可能になります。
6+
7+
!CODEFILE "../../src/jQuery/greenify.js"
8+
9+
実際に利用するためには、`jquery.js`を読み込んだ後に`greenify.js`を読み込ませる必要があります。
10+
11+
```html
12+
<script src="jquery.js"></script>
13+
<script src="greenify.js"></script>
14+
```
15+
16+
## どういう仕組み?

Diff for: package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"eslint": "eslint src/**/*.js",
2525
"eslint:md": "eslint --ext .md ja/**/*.md",
2626
"textlint": "summary-to-path | xargs textlint --rule spellcheck-tech-word",
27-
"test": "npm run textlint && npm run eslint:md && npm run eslint && npm run build"
27+
"test": "mocha --recursive && npm run textlint && npm run eslint:md && npm run eslint && npm run build"
2828
},
2929
"keywords": [
3030
"plugin",
@@ -33,9 +33,16 @@
3333
"devDependencies": {
3434
"eslint": "^1.3.0",
3535
"eslint-plugin-markdown": "git://github.com/eslint/eslint-plugin-markdown.git",
36+
"espower-babel": "^3.3.0",
3637
"gitbook-cli": "^0.3.4",
3738
"gitbook-summary-to-path": "^1.0.1",
39+
"jsdom": "^3.0.0",
40+
"mocha": "^2.2.5",
41+
"power-assert": "^1.0.0",
3842
"textlint": "^3.2.0",
3943
"textlint-rule-spellcheck-tech-word": "^4.0.1"
44+
},
45+
"dependencies": {
46+
"jquery": "^2.1.4"
4047
}
4148
}

Diff for: src/jQuery/greenify.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
(function ($) {
3+
$.fn.greenify = function () {
4+
this.css("color", "green");
5+
return this;
6+
};
7+
})(jQuery);

Diff for: src/jQuery/jquery.js

-3
This file was deleted.

Diff for: src/jQuery/package.json

-22
This file was deleted.

Diff for: test/jQuery/fixtures/testbed.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
9+
</body>
10+
</html>

Diff for: test/jQuery/greenify-test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import jsdom from "jsdom";
4+
import assert from "power-assert";
5+
import fs from "fs";
6+
const testbed = fs.readFileSync(__dirname + "/fixtures/testbed.html", "utf-8");
7+
const jquery = fs.readFileSync(__dirname + "/../../node_modules/jquery/dist/jquery.js", "utf-8");
8+
const greenify = fs.readFileSync(__dirname + "/../../src/jQuery/greenify.js", "utf-8");
9+
describe("greenify", function () {
10+
var $, document;
11+
before(done => {
12+
jsdom.env({
13+
html: testbed,
14+
src: [jquery, greenify],
15+
done: function (err, window) {
16+
document = window.document;
17+
$ = window.$;
18+
done();
19+
}
20+
});
21+
});
22+
it("should extend $.prototype with greenify", function () {
23+
assert(typeof $ !== "undefined");
24+
assert($.fn.greenify != null);
25+
assert($(document.body).greenify != null);
26+
assert($(document.body).greenify() instanceof $);
27+
});
28+
});

Diff for: test/mocha.opts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--compilers js:espower-babel/guess

0 commit comments

Comments
 (0)