Skip to content

Commit

Permalink
Merge pull request #34 from thoov/controller-link-to
Browse files Browse the repository at this point in the history
Add link-to with controller support
  • Loading branch information
thoov authored Dec 20, 2017
2 parents fc694a3 + e8be357 commit e1c6d85
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 41 deletions.
3 changes: 2 additions & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module.exports = function(/* environment, appConfig */) {
_ENABLE_IMMEDIATE_OBSERVER_SUPPORT: true,
_ENABLE_STRING_FMT_SUPPORT: true,
_ENABLE_FREEZABLE_SUPPORT: true,
_ENABLE_COMPONENT_DEFAULTLAYOUT_SUPPORT: true
_ENABLE_COMPONENT_DEFAULTLAYOUT_SUPPORT: true,
_ENABLE_CONTROLLER_WRAPPED_SUPPORT: true
}
};
};
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
this.import('vendor/reversed-observer.js');
this.import('vendor/initializer-arity.js');
this.import('vendor/router-resource.js');
this.import('vendor/current-when.js');
this.import('vendor/link-to.js');
this.import('vendor/deprecated-registry.js');
this.import('vendor/immediate-observer.js');
this.import('vendor/string-fmt.js');
Expand Down
20 changes: 0 additions & 20 deletions tests/acceptance/current-when-test.js

This file was deleted.

41 changes: 41 additions & 0 deletions tests/acceptance/link-to-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { test } from 'qunit';
import Controller from '@ember/controller';
import hbs from 'htmlbars-inline-precompile';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | {{link-to currentWhen="index"}} & {{#link-to "profile" otherController}}');

test('that {{link-to}} helper supports currentWhen', function(assert) {
visit('/about');

andThen(function() {
assert.equal(find('#other-link.active').length, 1, 'The link is active since current-when is a parent route');
});
});

test('that currentWhen will trigger a deprecation', function(assert) {
visit('/about');

andThen(function() {
assert.expectDeprecation('Usage of `currentWhen` is deprecated, use `current-when` instead.');
});
});

test('unwraps controllers', function(assert) {
this.application.register('template:link-to', hbs`{{#link-to 'profile' otherController}}Text{{/link-to}}`);
this.application.register('controller:link-to', Controller.extend({
otherController: Controller.create({
model: 'foo'
})
}));

let deprecation = /Providing `{{link-to}}` with a param that is wrapped in a controller is deprecated./;

visit('/link-to');

andThen(function() {
assert.expectDeprecation(deprecation);
let text = find('a').text().trim();
assert.equal(text, 'Text', 'The component is composed correctly');
});
});
2 changes: 2 additions & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Router.map(function() {
});
this.route('item');
this.route('defaultlayout');
this.route('link-to');
this.route('profile', { path: '/profile/:id' });
});

export default Router;
19 changes: 0 additions & 19 deletions vendor/current-when.js

This file was deleted.

49 changes: 49 additions & 0 deletions vendor/link-to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(function() {
var _Ember;
if (typeof Ember !== 'undefined') {
_Ember = Ember;
} else {
_Ember = require('ember').default;
}

const reopenObject = {
currentWhen: _Ember.computed.deprecatingAlias('current-when', {
id: 'ember-routing-view.deprecated-current-when',
until: '3.0.0'
}),

_getModels(params) {
let modelCount = params.length - 1;
let models = new Array(modelCount);

for (let i = 0; i < modelCount; i++) {
let value = params[i + 1];

while (_Ember.ControllerMixin.detect(value)) {
_Ember.deprecate(
'Providing `{{link-to}}` with a param that is wrapped in a controller is deprecated. ' +
(this.parentView ? 'Please update `' + this.parentView +
'` to use `{{link-to "post" someController.model}}` instead.' : ''),
false,
{ id: 'ember-routing-views.controller-wrapped-param', until: '3.0.0' },
);
value = value.get('model');
}

models[i] = value;
}

return models;
}
}

if (EmberENV && EmberENV._ENABLE_CURRENT_WHEN_SUPPORT !== true) {
delete reopenObject['currentWhen'];
}

if (EmberENV && EmberENV._ENABLE_CONTROLLER_WRAPPED_SUPPORT !== true) {
delete reopenObject['_getModels'];
}

_Ember.LinkComponent.reopen(reopenObject);
})();

0 comments on commit e1c6d85

Please sign in to comment.