Skip to content

Add support for Component.$() as part of RFC386 #27

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
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ branches:

jobs:
fail_fast: true
allow_failures:
- env: EMBER_TRY_SCENARIO=ember-canary

include:
# runs linting and tests with current locked deps
Expand Down
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';

const EMBER_VERSION_WITH_JQUERY_DEPRECATION = '3.9.0-canary';

module.exports = {
name: require('./package').name,
included() {
this._super.included.apply(this, arguments);

const VersionChecker = require('ember-cli-version-checker');

let app = this._findHost();
let optionalFeatures = app.project.findAddonByName("@ember/optional-features");

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

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

let checker = new VersionChecker(this);
let ember = checker.forEmber();

if (ember.gte(EMBER_VERSION_WITH_JQUERY_DEPRECATION)) {
app.import('vendor/jquery/component.dollar.js');
}

if (optionalFeatures && !optionalFeatures.isFeatureEnabled('jquery-integration')) {
app.project.ui.writeDeprecateLine('You have disabled the `jquery-integration` optional feature. You now have to delete `@ember/jquery` from your package.json');
}
Expand All @@ -24,16 +36,16 @@ module.exports = {
const resolve = require('resolve');
const path = require('path');

var jqueryPath;
let jqueryPath;
try {
jqueryPath = path.dirname(
resolve.sync('jquery/package.json', { basedir: this.project.root })
);
} catch (error) {
} catch(error) {
jqueryPath = path.dirname(require.resolve('jquery/package.json'));
}

var jquery = new Funnel(jqueryPath + '/dist', {
let jquery = new Funnel(jqueryPath + '/dist', {
destDir: 'jquery',
files: ['jquery.js'],
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"broccoli-funnel": "^2.0.1",
"broccoli-merge-trees": "^3.0.2",
"ember-cli-babel": "^7.4.0",
"ember-cli-version-checker": "^3.0.0",
"jquery": "^3.3.1",
"resolve": "^1.10.0"
},
Expand Down
Empty file removed tests/integration/.gitkeep
Empty file.
39 changes: 39 additions & 0 deletions tests/integration/components/component-dot-dollar-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import Component from '@ember/component';
import jQuery from 'jquery';

const component = Component.extend({
didInsertElement() {
this.setJQueryElement(this.$(this.get('selector')));
}
});

module('Integration | Component | component-dot-dollar', function(hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function() {
this.owner.register('component:jquery-component', component);
this.$element = null;
this.setJQueryElement = ($) => this.$element = $;
});

test('it implements Component.$()', function(assert) {
return assert.noDeprecations(async () => {
await render(hbs`{{jquery-component id="jq" setJQueryElement=setJQueryElement}}`);

assert.ok(this.$element, 'Component.$() is available');
assert.ok(this.$element instanceof jQuery, 'Component.$() returns a jQuery object');
assert.equal(this.$element.get(0), this.element.querySelector('#jq'), 'Component.$() is a jQuery wrapper around Component.element');
});
});

test('it implements Component.$(selector)', function(assert) {
return assert.noDeprecations(async () => {
await render(hbs`{{#jquery-component id="jq" selector="div" setJQueryElement=setJQueryElement}}<div id="child"/>{{/jquery-component}}`);

assert.equal(this.$element.get(0), this.element.querySelector('#child'), 'Component.$(selector) is a jQuery object of the child elements matching selector');
});
});
});
28 changes: 28 additions & 0 deletions tests/test-helper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import QUnit from 'qunit';
import { registerDeprecationHandler } from '@ember/debug';
import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';

let deprecations;

registerDeprecationHandler((message, options, next) => {
// in case a deprecation is issued before a test is started
if (!deprecations) {
deprecations = [];
}

deprecations.push(message);
next(message, options);
});

QUnit.testStart(function() {
deprecations = [];
});

QUnit.assert.noDeprecations = async function(callback) {
let originalDeprecations = deprecations;
deprecations = [];

await callback();
this.deepEqual(deprecations, [], 'Expected no deprecations during test.');

deprecations = originalDeprecations;
};

setApplication(Application.create(config.APP));

start();
12 changes: 12 additions & 0 deletions vendor/jquery/component.dollar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Ember.Component.reopen({
$(sel) {
Ember.assert(
"You cannot access this.$() on a component with `tagName: ''` specified.",
this.tagName !== ''
);

if (this.element) {
return sel ? jQuery(sel, this.element) : jQuery(this.element);
}
}
});