Skip to content

Commit

Permalink
[BUGFIX beta] Make link-to a real component.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhammond committed Aug 22, 2015
1 parent 116016c commit c8bdb32
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { get } from 'ember-metal/property_get';
import { set } from 'ember-metal/property_set';
import setProperties from 'ember-metal/set_properties';
import { MUTABLE_CELL } from 'ember-views/compat/attrs-proxy';
import { HAS_BLOCK } from 'ember-views/system/link-to';
import { instrument } from 'ember-htmlbars/system/instrumentation-support';
import LegacyEmberComponent from 'ember-views/components/component';
import GlimmerComponent from 'ember-htmlbars/glimmer-component';
Expand Down Expand Up @@ -45,7 +46,10 @@ ComponentNodeManager.create = function(renderNode, env, options) {

component = component || (isAngleBracket ? GlimmerComponent : LegacyEmberComponent);

let createOptions = { parentView };
let createOptions = {
parentView,
[HAS_BLOCK]: !!templates.default
};

configureTagName(attrs, tagName, component, isAngleBracket, createOptions);

Expand All @@ -65,8 +69,8 @@ ComponentNodeManager.create = function(renderNode, env, options) {
// Instantiate the component
component = createComponent(component, isAngleBracket, createOptions, renderNode, env, attrs);

// If the component specifies its template via the `layout properties
// instead of using the template looked up in the container, get them
// If the component specifies its layout via the `layout` property
// instead of using the template looked up in the container, get it
// now that we have the component instance.
layout = get(component, 'layout') || layout;

Expand Down
21 changes: 7 additions & 14 deletions packages/ember-routing-htmlbars/lib/keywords/link-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
@submodule ember-routing-htmlbars
*/

import { readArray } from 'ember-metal/streams/utils';
import Ember from 'ember-metal/core'; // assert
import merge from 'ember-metal/merge';
import { RELATED_VIEW } from 'ember-views/system/link-to';

/**
The `{{link-to}}` helper renders a link to the supplied
Expand Down Expand Up @@ -315,19 +314,13 @@ export default {
},

render(morph, env, scope, params, hash, template, inverse, visitor) {
var attrs = merge({}, hash);

// TODO: Rewrite link-to to use arbitrary length positional params.
attrs.params = readArray(params);

// Used for deprecations (to tell the user what view the deprecated syntax
// was used in).
attrs.view = env.view;

// TODO: Remove once `hasBlock` is working again
attrs.hasBlock = !!template;
Ember.runInDebug(() => {
// Used for deprecations (to tell the user what view the deprecated syntax was used in).
// Until: 3.0.0
hash[RELATED_VIEW] = env.view;
});

env.hooks.component(morph, env, scope, '-link-to', params, attrs, { default: template }, visitor);
env.hooks.component(morph, env, scope, '-link-to', params, hash, { default: template }, visitor);
},

rerender(morph, env, scope, params, hash, template, inverse, visitor) {
Expand Down
13 changes: 13 additions & 0 deletions packages/ember-routing-htmlbars/tests/helpers/link-to_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ registry.register('service:-routing', EmberObject.extend({

registry.register('component-lookup:main', ComponentLookup);
registry.register('component:-link-to', LinkComponent);
registry.register('component:custom-link-to', LinkComponent.extend());

QUnit.module('ember-routing-htmlbars: link-to helper', {
setup() {
Expand Down Expand Up @@ -145,3 +146,15 @@ QUnit.test('unwraps controllers', function() {

equal(view.$().text(), 'Text');
});

QUnit.test('able to safely extend the built-in component and use the normal path', function() {
view = EmberView.create({
title: 'my custom link-to component',
template: compile('{{custom-link-to view.title}}'),
container: container
});

runAppend(view);

equal(view.$().text(), 'my custom link-to component', 'rendered a custom-link-to component');
});
12 changes: 8 additions & 4 deletions packages/ember-routing-views/lib/views/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import EmberComponent from 'ember-views/components/component';
import inject from 'ember-runtime/inject';
import 'ember-runtime/system/service'; // creates inject.service
import ControllerMixin from 'ember-runtime/mixins/controller';
import { HAS_BLOCK, RELATED_VIEW } from 'ember-views/system/link-to';

import linkToTemplate from 'ember-htmlbars/templates/link-to';
linkToTemplate.meta.revision = 'Ember@VERSION_STRING_PLACEHOLDER';
Expand Down Expand Up @@ -365,7 +366,7 @@ var LinkComponent = EmberComponent.extend({
if (lastParam && lastParam.isQueryParams) {
params.pop();
}
let onlyQueryParamsSupplied = (this.attrs.hasBlock ? params.length === 0 : params.length === 1);
let onlyQueryParamsSupplied = (this[HAS_BLOCK] ? params.length === 0 : params.length === 1);
if (onlyQueryParamsSupplied) {
var appController = this.container.lookup('controller:application');
if (appController) {
Expand Down Expand Up @@ -416,8 +417,7 @@ var LinkComponent = EmberComponent.extend({
this.set('disabled', attrs.disabledWhen);
}

// TODO: Change to built-in hasBlock once it's available
if (!attrs.hasBlock) {
if (!this[HAS_BLOCK]) {
this.set('linkTitle', params.shift());
}

Expand All @@ -431,7 +431,7 @@ var LinkComponent = EmberComponent.extend({
while (ControllerMixin.detect(value)) {
Ember.deprecate(
'Providing `{{link-to}}` with a param that is wrapped in a controller is deprecated. ' +
'Please update `' + attrs.view + '` to use `{{link-to "post" someController.model}}` instead.',
(attrs[RELATED_VIEW] ? 'Please update `' + attrs[RELATED_VIEW] + '` to use `{{link-to "post" someController.model}}` instead.' : ''),
false,
{ id: 'ember-routing-views.controller-wrapped-param', until: '3.0.0' }
);
Expand Down Expand Up @@ -503,4 +503,8 @@ function getResolvedQueryParams(queryParamsObject, targetRouteName) {
return resolvedQueryParams;
}

LinkComponent.reopenClass({
positionalParams: 'params'
});

export default LinkComponent;
5 changes: 5 additions & 0 deletions packages/ember-views/lib/system/link-to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { symbol } from 'ember-metal/utils';

// These symbols will be used to limit link-to's public API surface area.
export let HAS_BLOCK = symbol('HAS_BLOCK');
export let RELATED_VIEW = symbol('RELATED_VIEW');

0 comments on commit c8bdb32

Please sign in to comment.