Skip to content

Commit c7e9bf4

Browse files
authored
Merge pull request #27 from simonihmig/component.dollar
Add support for Component.$() as part of RFC386
2 parents c6fffbf + c1e640c commit c7e9bf4

File tree

7 files changed

+107
-6
lines changed

7 files changed

+107
-6
lines changed

.travis.yml

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ branches:
2727

2828
jobs:
2929
fail_fast: true
30-
allow_failures:
31-
- env: EMBER_TRY_SCENARIO=ember-canary
3230

3331
include:
3432
# runs linting and tests with current locked deps

index.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
'use strict';
22

3+
const EMBER_VERSION_WITH_JQUERY_DEPRECATION = '3.9.0-alpha.1';
4+
35
module.exports = {
46
name: require('./package').name,
57
included() {
68
this._super.included.apply(this, arguments);
9+
10+
const VersionChecker = require('ember-cli-version-checker');
11+
712
let app = this._findHost();
813
let optionalFeatures = app.project.findAddonByName("@ember/optional-features");
914

@@ -13,6 +18,13 @@ module.exports = {
1318

1419
app.import('vendor/shims/jquery.js');
1520

21+
let checker = new VersionChecker(this);
22+
let ember = checker.forEmber();
23+
24+
if (ember.gte(EMBER_VERSION_WITH_JQUERY_DEPRECATION)) {
25+
app.import('vendor/jquery/component.dollar.js');
26+
}
27+
1628
if (optionalFeatures && !optionalFeatures.isFeatureEnabled('jquery-integration')) {
1729
app.project.ui.writeDeprecateLine('You have disabled the `jquery-integration` optional feature. You now have to delete `@ember/jquery` from your package.json');
1830
}
@@ -24,20 +36,27 @@ module.exports = {
2436
const resolve = require('resolve');
2537
const path = require('path');
2638

27-
var jqueryPath;
39+
let jqueryPath;
2840
try {
2941
jqueryPath = path.dirname(
3042
resolve.sync('jquery/package.json', { basedir: this.project.root })
3143
);
32-
} catch (error) {
44+
} catch(error) {
3345
jqueryPath = path.dirname(require.resolve('jquery/package.json'));
3446
}
3547

36-
var jquery = new Funnel(jqueryPath + '/dist', {
48+
let jquery = new Funnel(jqueryPath + '/dist', {
3749
destDir: 'jquery',
3850
files: ['jquery.js'],
3951
});
4052

41-
return new BroccoliMergeTrees([jquery, tree]);
53+
let babelAddon = this.project.findAddonByName('ember-cli-babel');
54+
let transpiledTree = babelAddon.transpileTree(tree, {
55+
'ember-cli-babel': {
56+
compileModules: false
57+
}
58+
});
59+
60+
return new BroccoliMergeTrees([jquery, transpiledTree]);
4261
},
4362
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"broccoli-funnel": "^2.0.1",
2424
"broccoli-merge-trees": "^3.0.2",
2525
"ember-cli-babel": "^7.4.0",
26+
"ember-cli-version-checker": "^3.0.0",
2627
"jquery": "^3.3.1",
2728
"resolve": "^1.10.0"
2829
},

tests/integration/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { module, test } from 'qunit';
2+
import { setupRenderingTest } from 'ember-qunit';
3+
import { render } from '@ember/test-helpers';
4+
import hbs from 'htmlbars-inline-precompile';
5+
import Component from '@ember/component';
6+
import jQuery from 'jquery';
7+
8+
const component = Component.extend({
9+
didInsertElement() {
10+
this.setJQueryElement(this.$(this.get('selector')));
11+
}
12+
});
13+
14+
module('Integration | Component | component-dot-dollar', function(hooks) {
15+
setupRenderingTest(hooks);
16+
hooks.beforeEach(function() {
17+
this.owner.register('component:jquery-component', component);
18+
this.$element = null;
19+
this.setJQueryElement = ($) => this.$element = $;
20+
});
21+
22+
test('it implements Component.$()', function(assert) {
23+
return assert.noDeprecations(async () => {
24+
await render(hbs`{{jquery-component id="jq" setJQueryElement=setJQueryElement}}`);
25+
26+
assert.ok(this.$element, 'Component.$() is available');
27+
assert.ok(this.$element instanceof jQuery, 'Component.$() returns a jQuery object');
28+
assert.equal(this.$element.get(0), this.element.querySelector('#jq'), 'Component.$() is a jQuery wrapper around Component.element');
29+
});
30+
});
31+
32+
test('it implements Component.$(selector)', function(assert) {
33+
return assert.noDeprecations(async () => {
34+
await render(hbs`{{#jquery-component id="jq" selector="div" setJQueryElement=setJQueryElement}}<div id="child"/>{{/jquery-component}}`);
35+
36+
assert.equal(this.$element.get(0), this.element.querySelector('#child'), 'Component.$(selector) is a jQuery object of the child elements matching selector');
37+
});
38+
});
39+
});

tests/test-helper.js

+28
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
1+
import QUnit from 'qunit';
2+
import { registerDeprecationHandler } from '@ember/debug';
13
import Application from '../app';
24
import config from '../config/environment';
35
import { setApplication } from '@ember/test-helpers';
46
import { start } from 'ember-qunit';
57

8+
let deprecations;
9+
10+
registerDeprecationHandler((message, options, next) => {
11+
// in case a deprecation is issued before a test is started
12+
if (!deprecations) {
13+
deprecations = [];
14+
}
15+
16+
deprecations.push(message);
17+
next(message, options);
18+
});
19+
20+
QUnit.testStart(function() {
21+
deprecations = [];
22+
});
23+
24+
QUnit.assert.noDeprecations = async function(callback) {
25+
let originalDeprecations = deprecations;
26+
deprecations = [];
27+
28+
await callback();
29+
this.deepEqual(deprecations, [], 'Expected no deprecations during test.');
30+
31+
deprecations = originalDeprecations;
32+
};
33+
634
setApplication(Application.create(config.APP));
735

836
start();

vendor/jquery/component.dollar.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { assert } from '@ember/debug';
2+
3+
(function() {
4+
Ember.Component.reopen({
5+
$(sel) {
6+
assert(
7+
"You cannot access this.$() on a component with `tagName: ''` specified.",
8+
this.tagName !== ''
9+
);
10+
11+
if (this.element) {
12+
return sel ? jQuery(sel, this.element) : jQuery(this.element);
13+
}
14+
}
15+
});
16+
})();

0 commit comments

Comments
 (0)