Skip to content

jQueryプラグインのサンプル実装 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"es6": true,
"node": true,
"browser": true,
"jquery": true,
"mocha": true
},
"ecmaFeatures": {
Expand Down
20 changes: 14 additions & 6 deletions ja/jQuery/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# jQueryのPlugin Pattern
# jQueryのPlugin

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

```js
var jQuery = require("jquery");
jQuery(document.body);
```
次の`greenify`プラグインでは、`$(document.body).greenify();`というメソッド呼び出しが可能になります。

!CODEFILE "../../src/jQuery/greenify.js"

実際に利用するためには、`jquery.js`を読み込んだ後に`greenify.js`を読み込ませる必要があります。

```html
<script src="jquery.js"></script>
<script src="greenify.js"></script>
```

## どういう仕組み?
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"eslint": "eslint src/**/*.js",
"eslint:md": "eslint --ext .md ja/**/*.md",
"textlint": "summary-to-path | xargs textlint --rule spellcheck-tech-word",
"test": "npm run textlint && npm run eslint:md && npm run eslint && npm run build"
"test": "mocha --recursive && npm run textlint && npm run eslint:md && npm run eslint && npm run build"
},
"keywords": [
"plugin",
Expand All @@ -33,9 +33,16 @@
"devDependencies": {
"eslint": "^1.3.0",
"eslint-plugin-markdown": "git://github.com/eslint/eslint-plugin-markdown.git",
"espower-babel": "^3.3.0",
"gitbook-cli": "^0.3.4",
"gitbook-summary-to-path": "^1.0.1",
"jsdom": "^3.0.0",
"mocha": "^2.2.5",
"power-assert": "^1.0.0",
"textlint": "^3.2.0",
"textlint-rule-spellcheck-tech-word": "^4.0.1"
},
"dependencies": {
"jquery": "^2.1.4"
}
}
7 changes: 7 additions & 0 deletions src/jQuery/greenify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
(function ($) {
$.fn.greenify = function () {
this.css("color", "green");
return this;
};
})(jQuery);
3 changes: 0 additions & 3 deletions src/jQuery/jquery.js

This file was deleted.

22 changes: 0 additions & 22 deletions src/jQuery/package.json

This file was deleted.

10 changes: 10 additions & 0 deletions test/jQuery/fixtures/testbed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>

</body>
</html>
28 changes: 28 additions & 0 deletions test/jQuery/greenify-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// LICENSE : MIT
"use strict";
import jsdom from "jsdom";
import assert from "power-assert";
import fs from "fs";
const testbed = fs.readFileSync(__dirname + "/fixtures/testbed.html", "utf-8");
const jquery = fs.readFileSync(__dirname + "/../../node_modules/jquery/dist/jquery.js", "utf-8");
const greenify = fs.readFileSync(__dirname + "/../../src/jQuery/greenify.js", "utf-8");
describe("greenify", function () {
var $, document;
before(done => {
jsdom.env({
html: testbed,
src: [jquery, greenify],
done: function (err, window) {
document = window.document;
$ = window.$;
done();
}
});
});
it("should extend $.prototype with greenify", function () {
assert(typeof $ !== "undefined");
assert($.fn.greenify != null);
assert($(document.body).greenify != null);
assert($(document.body).greenify() instanceof $);
});
});
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--compilers js:espower-babel/guess