├── .gitignore ├── package.json ├── jquery.readyselector.js ├── README.md ├── MIT_LICENSE └── test └── jquery.readyselector_spec.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "John Firebaugh ", 3 | "name": "jquery-readyselector", 4 | "description": "Extends `$().ready()` to provide a convenient syntax for page-specific script", 5 | "version": "0.1.0", 6 | "main": "./jquery.readyselector", 7 | "devDependencies": { 8 | "mocha": "*", 9 | "chai": "*", 10 | "sinon": "*", 11 | "sinon-chai": "*", 12 | "jsdom": "*", 13 | "jquery": "*" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /jquery.readyselector.js: -------------------------------------------------------------------------------- 1 | (function ($) { 2 | var ready = $.fn.ready; 3 | $.fn.ready = function (fn) { 4 | if (this.context === undefined) { 5 | // The $().ready(fn) case. 6 | ready(fn); 7 | } else if (this.selector) { 8 | ready($.proxy(function(){ 9 | $(this.selector, this.context).each(fn); 10 | }, this)); 11 | } else { 12 | ready($.proxy(function(){ 13 | $(this).each(fn); 14 | }, this)); 15 | } 16 | } 17 | })(jQuery); 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | With the Rails asset pipeline or other asset packagers, you 2 | usually include the JS for your entire application in one bundle, 3 | while individual scripts should be run only on certain pages. 4 | 5 | jquery.readyselector extends `.ready()` to provide a nice syntax 6 | for page-specific script: 7 | 8 | ```javascript 9 | $('.posts.index').ready(function () { 10 | // ... 11 | }); 12 | ``` 13 | 14 | This works well if you include the controller name and action 15 | as `` element classes, a la: 16 | http://postpostmodern.com/2009/02/20/a-body-with-class/ 17 | 18 | The callback will be run once for each matching element, with 19 | the usual 'this' context for a jQuery callback. `$(fn)`, 20 | `$(document).ready(fn)`, and `$().ready(fn)` behave as normal. 21 | -------------------------------------------------------------------------------- /MIT_LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 Verba Software, Andrew Peterson 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /test/jquery.readyselector_spec.js: -------------------------------------------------------------------------------- 1 | var chai = require("chai"), 2 | sinon = require("sinon"), 3 | sinonChai = require("sinon-chai"), 4 | jsdom = require("jsdom"), 5 | jQuery = require("jQuery"); 6 | 7 | chai.should(); 8 | chai.use(sinonChai); 9 | 10 | var window = jsdom.jsdom().createWindow(), 11 | document = window.document; 12 | 13 | var $ = global.jQuery = jQuery.create(window); 14 | $('body').addClass('body-class'); 15 | 16 | require("../jquery.readyselector"); 17 | 18 | describe("$.fn.ready", function() { 19 | beforeEach(function() { 20 | this.callback = sinon.spy(); 21 | }); 22 | 23 | it("calls the callback when bound with $(document).ready(@callback)", function() { 24 | $(document).ready(this.callback); 25 | this.callback.should.have.been.called; 26 | this.callback.thisValues.should.eql([document]); 27 | }); 28 | 29 | it("calls the callback when bound with $(@callback)", function() { 30 | $(this.callback); 31 | this.callback.should.have.been.called; 32 | this.callback.thisValues.should.eql([document]); 33 | }); 34 | 35 | it("calls the callback when bound with $().ready(@callback)", function() { 36 | $().ready(this.callback); 37 | this.callback.should.have.been.called; 38 | this.callback.thisValues.should.eql([document]); 39 | }); 40 | 41 | it("calls the callback if the given selector is present", function() { 42 | $('.body-class').ready(this.callback); 43 | this.callback.should.have.been.called; 44 | }); 45 | 46 | it("does not call the callback if the given selector is not present", function() { 47 | $('#nonesuch').ready(this.callback); 48 | this.callback.should.not.have.been.called; 49 | }); 50 | 51 | it("calls the callback with the correct context", function() { 52 | $('.body-class').ready(this.callback); 53 | this.callback.thisValues.should.eql($('body').get()); 54 | }); 55 | 56 | it("calls the callback for each matching element", function() { 57 | $('body').html('
'); 58 | $('body div').ready(this.callback); 59 | this.callback.thisValues.should.eql($('body div').get()); 60 | }); 61 | 62 | it("calls the callback for a delayed selection", function() { 63 | var selection = $('nonesuch'); 64 | selection.selector = '.body-class'; 65 | selection.ready(this.callback); 66 | this.callback.thisValues.should.eql($('body').get()); 67 | }); 68 | }); 69 | --------------------------------------------------------------------------------