index.html
: <html> <head> <meta charset="utf-8"/> <title> -</title> <script src="jquery.js"></script> <script src="https://rawgithub.com/tenorok/definer/master/definer.js"></script> <script src="modules/cart.js"></script> <script src="modules/list.js"></script> </head> <body> <ul class="list"> <li class="item"> <span class="name"></span>, <span class="price">250</span> </li> <li class="item"> <span class="name"></span>, <span class="price">100</span> </li> <li class="item"> <span class="name"></span>, <span class="price">300</span> </li> </ul> </body> </html>
Cart
module that implements an online store cart with the ability to add products and get the total value of the added goods: definer('Cart', function() { function Cart() { this.list = []; } Cart.prototype = { add: function(target) { var item = $(target); this.list.push({ name: item.find('.name').text(), price: +item.find('.price').text() }); }, sum: function() { return this.list.reduce(function(sum, item) { return sum + item.price; }, 0); } }; return Cart; });
list
module, which depends on the Cart
and implements the interaction of the visitor with the catalog: definer('list', function(Cart) { var iCart = new Cart(); $(function() { $('.item').on('click', function(e) { iCart.add(e.currentTarget); console.log(iCart.sum()); }); }); });
modules/clean.js
in front of the existing modules: definer.clean('$');
$
variable is not in the global context: console.log($); // undefined
definer('Cart', function($) { ... }); definer('list', function($, Cart) { ... });
npm install definer
modules
directory into the index.js
file: ./node_modules/.bin/definer -d modules/ index.js
index.html
enough to connect only jQuery and the assembled file: <script src="jquery.js"></script> <script src="index.js"></script>
npm install grunt grunt-cli grunt-contrib-watch grunt-definer
Gruntfile.js
file to the project root: module.exports = function(grunt) { grunt.initConfig({ watch: { scripts: { files: ['modules/*.js'], tasks: ['definer:all'] }, }, definer: { all: { target: 'index.js', directory: 'modules/' } } }); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-definer'); };
./node_modules/.bin/grunt watch
clean
option to the grunt-target: definer: { all: { target: 'index.js', directory: 'modules/', clean: { $: 'jquery.js' } } }
index.html
enough to connect only one assembled file: <script src="index.js"></script>
jsdoc
option to the grunt goal: jsdoc: { "file": " -", "copyright": "2014 Artem Kurbatov, tenorok.ru", "license": "MIT license", "version": "package.json", "date": true }
package.json
file in the project root: { "version": "0.1.0" }
/*! * @file - * @copyright 2014 Artem Kurbatov, tenorok.ru * @license MIT license * @version 0.1.0 * @date 17 February 2014 */
Source: https://habr.com/ru/post/212817/
All Articles