Skip to content

Commit

Permalink
add jquery to legacy widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
mlrawlings committed Nov 30, 2017
1 parent ae2066f commit 867c656
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
10 changes: 7 additions & 3 deletions src/components/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ var ready = require('./ready');

var idRegExp = /^\#(\S+)( .*)?/;

exports.patchComponent = function(jQuery) {
exports.patchComponent = function(jQuery, proto, delayThrow) {
/* globals window */

if (!(jQuery || (jQuery = window.$))) {
if (!(jQuery || (jQuery = window.$)) && !delayThrow) {
throw new Error('jQuery not found');
}

require('./Component').prototype.$ = function jqueryProxy(arg) {
(proto || require('./Component').prototype).$ = function jqueryProxy(arg) {
var args = arguments;
var self = this;

if (!jQuery) {
throw new Error('jQuery not found');
}

if (args.length === 1) {
//Handle an "ondomready" callback function
if (typeof arg === 'function') {
Expand Down
10 changes: 9 additions & 1 deletion src/components/legacy/defineWidget-legacy-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
var BaseState;
var BaseComponent;
var inherit;

var jQuery = require('../jquery');
var ready = require('../ready');

module.exports = function defineWidget(def, renderer) {
def = def.Widget || def;
Expand Down Expand Up @@ -110,6 +111,13 @@ module.exports = function defineWidget(def, renderer) {
inherit(State, BaseState);
proto.___State = State;

jQuery.patchComponent(
window.$,
proto,
true /* don't throw error until used if `$` is missing*/
);

ready.patchComponent(proto);

if (!renderer) {
renderer = ComponentClass.renderer || ComponentClass.prototype.renderer;
Expand Down
4 changes: 2 additions & 2 deletions src/components/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ function ready(callback, thisObj, doc) {

module.exports = ready;

module.exports.patchComponent = function() {
require('./Component').prototype.ready = function (callback) {
module.exports.patchComponent = function(proto) {
(proto || require('./Component').prototype).ready = function (callback) {
var document = this.el.ownerDocument;
ready(callback, this, document);
};
Expand Down
13 changes: 1 addition & 12 deletions test/deprecated-components-browser/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,19 @@ if (typeof window !== 'undefined') {
describe('deprecated-components-browser', function () {
require('../__util__/autotest').runTests(require('./fixtures/autotests.tests'), function run(testFunc, done) {
var helpers = new BrowserHelpers();

require('marko/jquery').patchComponent(window.$);
require('marko/ready').patchComponent();

function cleanup() {
delete require('marko/components/Component').prototype.$;
delete require('marko/components/Component').prototype.ready;
}

try {
if (testFunc.length === 1) {
testFunc(helpers);
helpers._cleanup();
cleanup();
done();
} else {
testFunc(helpers, function (err) {
helpers._cleanup();
cleanup();
done(err);
});
}
} catch (e) {
cleanup();
helpers._cleanup();
throw e;
}
});
Expand Down

0 comments on commit 867c656

Please sign in to comment.