Skip to content

Commit

Permalink
[CLEANUP beta] Drop _registerHelper, fix unbound tests and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Aug 5, 2015
1 parent 26522af commit 4d10f6c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 101 deletions.
6 changes: 0 additions & 6 deletions packages/ember-htmlbars/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
@private
@property helpers
*/
import Ember from 'ember-metal/core';
import EmptyObject from 'ember-metal/empty_object';

var helpers = new EmptyObject();
Expand All @@ -28,9 +27,4 @@ export function registerHelper(name, helperFunc) {
helpers[name] = helperFunc;
}

export let deprecatedRegisterHelper = Ember.deprecateFunc(
'Using Ember.HTMLBars._registerHelper is deprecated. Helpers (even dashless ones) are automatically resolved.',
{ id: 'ember-htmlbars.register-helper', until: '2.0.0' },
registerHelper);

export default helpers;
2 changes: 1 addition & 1 deletion packages/ember-htmlbars/lib/keywords/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if (isEnabled('ember-htmlbars-get-helper')) {
};

var getKeyword = function getKeyword(morph, env, scope, params, hash, template, inverse, visitor) {
if (!morph) {
if (morph === null) {
return buildStream(params);
} else {
let stream;
Expand Down
36 changes: 18 additions & 18 deletions packages/ember-htmlbars/lib/keywords/unbound.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import Ember from 'ember-metal/core'; // assert

/**
@module ember
@submodule ember-htmlbars
*/

export default function unbound(morph, env, scope, originalParams, hash, template, inverse) {
// Since we already got the params as a set of streams, we need to extract the key from
// the first param instead of (incorrectly) trying to read from it. If this was a call
// to `{{unbound foo.bar}}`, then we pass along the original stream to `hooks.range`.
var params = originalParams.slice();
var valueStream = params.shift();
export default function unbound(morph, env, scope, params, hash, template, inverse, visitor) {
Ember.assert(
'unbound helper cannot be called with multiple params or hash params',
params.length === 1 && Object.keys(hash).length === 0
);
Ember.assert(
'unbound helper cannot be called as a block',
!template
);

// If `morph` is `null` the keyword is being invoked as a subexpression.
if (morph === null) {
if (originalParams.length > 1) {
valueStream = env.hooks.subexpr(env, scope, valueStream.key, params, hash);
}

return new VolatileStream(valueStream);
return new VolatileStream(params[0]);
}

if (params.length === 0) {
env.hooks.range(morph, env, scope, null, valueStream);
} else if (template === null) {
env.hooks.inline(morph, env, scope, valueStream.key, params, hash);
let stream;
if (morph.linkedResult) {
stream = morph.linkedResult;
} else {
env.hooks.block(morph, env, scope, valueStream.key, params, hash, template, inverse);
stream = new VolatileStream(params[0]);
morph.linkedResult = stream;
}

env.hooks.range(morph, env, scope, null, stream, visitor);
return true;
}

Expand Down
4 changes: 1 addition & 3 deletions packages/ember-htmlbars/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import {
import makeBoundHelper from 'ember-htmlbars/system/make_bound_helper';

import {
registerHelper,
deprecatedRegisterHelper
registerHelper
} from 'ember-htmlbars/helpers';
import {
ifHelper,
Expand Down Expand Up @@ -84,7 +83,6 @@ if (Ember.ENV._ENABLE_LEGACY_VIEW_SUPPORT) {
}

Ember.HTMLBars = {
_registerHelper: deprecatedRegisterHelper,
template: template,
compile: compile,
precompile: precompile,
Expand Down
16 changes: 8 additions & 8 deletions packages/ember-htmlbars/tests/helpers/if_unless_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ QUnit.test('The `if` helper tests for `isTruthy` if available', function() {
QUnit.test('The `if` helper does not error on undefined', function() {
view = EmberView.create({
undefinedValue: undefined,
template: compile('{{#if view.undefinedValue}}Yep{{/if}}{{#unbound if view.undefinedValue}}Yep{{/unbound}}')
template: compile('{{#if view.undefinedValue}}Yep{{/if}}{{#if (unbound view.undefinedValue)}}Yep{{/if}}')
});

runAppend(view);
Expand All @@ -71,7 +71,7 @@ QUnit.test('The `if` helper does not error on undefined', function() {
QUnit.test('The `unless` helper does not error on undefined', function() {
view = EmberView.create({
undefinedValue: undefined,
template: compile('{{#unless view.undefinedValue}}YepBound{{/unless}}{{#unbound unless view.undefinedValue}}YepUnbound{{/unbound}}')
template: compile('{{#unless view.undefinedValue}}YepBound{{/unless}}{{#unless (unbound view.undefinedValue)}}YepUnbound{{/unless}}')
});

runAppend(view);
Expand Down Expand Up @@ -185,10 +185,10 @@ QUnit.test('The `if` helper updates when the value changes', function() {
equal(view.$().text(), '');
});

QUnit.test('The `unbound if` helper does not update when the value changes', function() {
QUnit.test('The `if (unbound` helper does not update when the value changes', function() {
view = EmberView.create({
conditional: true,
template: compile('{{#unbound if view.conditional}}Yep{{/unbound}}')
template: compile('{{#if (unbound view.conditional)}}Yep{{/if}}')
});
runAppend(view);
equal(view.$().text(), 'Yep');
Expand All @@ -211,10 +211,10 @@ QUnit.test('The `unless` helper updates when the value changes', function() {
equal(view.$().text(), '');
});

QUnit.test('The `unbound if` helper does not update when the value changes', function() {
QUnit.test('The `if (unbound` helper does not update when the value changes', function() {
view = EmberView.create({
conditional: false,
template: compile('{{#unbound unless view.conditional}}Nope{{/unbound}}')
template: compile('{{#unless (unbound view.conditional)}}Nope{{/unless}}')
});
runAppend(view);
equal(view.$().text(), 'Nope');
Expand All @@ -224,10 +224,10 @@ QUnit.test('The `unbound if` helper does not update when the value changes', fun
equal(view.$().text(), 'Nope');
});

QUnit.test('The `unbound if` helper should work when its inverse is not present', function() {
QUnit.test('The `if (unbound` helper should work when its inverse is not present', function() {
view = EmberView.create({
conditional: false,
template: compile('{{#unbound if view.conditional}}Yep{{/unbound}}')
template: compile('{{#if (unbound view.conditional)}}Yep{{/if}}')
});
runAppend(view);
equal(view.$().text(), '');
Expand Down
96 changes: 41 additions & 55 deletions packages/ember-htmlbars/tests/helpers/unbound_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*jshint newcap:false*/
import EmberView from 'ember-views/views/view';
import EmberComponent from 'ember-views/views/component';
import EmberObject from 'ember-runtime/system/object';

import { A } from 'ember-runtime/system/native_array';
Expand Down Expand Up @@ -53,15 +54,15 @@ QUnit.test('it should not re-render if the property changes', function() {
equal(view.$().text(), 'BORK BORK', 'should not re-render if the property changes');
});

QUnit.test('it should re-render if the parent view rerenders', function() {
QUnit.test('it should not re-render if the parent view rerenders', function() {
run(function() {
view.set('context.foo', 'OOF');
view.rerender();
});
equal(view.$().text(), 'OOF OOF', 'should re-render if the parent view rerenders');
});

QUnit.test('it should throw the helper missing error if multiple properties are provided', function() {
QUnit.test('it should assert unbound cannot be called with multiple arguments', function() {
expectAssertion(function() {
runAppend(EmberView.create({
template: compile('{{unbound foo bar}}'),
Expand All @@ -70,7 +71,7 @@ QUnit.test('it should throw the helper missing error if multiple properties are
bar: 'foo'
})
}));
}, /A helper named 'foo' could not be found/);
}, /unbound helper cannot be called with multiple params or hash params/);
});

QUnit.test('should property escape unsafe hrefs', function() {
Expand Down Expand Up @@ -103,6 +104,24 @@ QUnit.test('should property escape unsafe hrefs', function() {
}
});

QUnit.test('should render on attributes', function(assert) {
/* jshint scripturl:true */

runDestroy(view);

view = EmberComponent.create({
layout: compile('<a href={{unbound name}}></a>'),
name: 'bob'
});

runAppend(view);

run(function() {
view.set('name', 'rick');
});
assert.equal(view.$().html(), '<a href="bob"></a>');
});

QUnit.module('ember-htmlbars: {{#unbound}} helper with container present', {
setup() {
Ember.lookup = lookup = { Ember: Ember };
Expand Down Expand Up @@ -171,12 +190,12 @@ QUnit.test('it should not re-render if the property changes', function() {
equal(view.$().text(), 'BORK', 'should not re-render if the property changes');
});

QUnit.test('it should re-render if the parent view rerenders', function() {
QUnit.test('it should not re-render if the parent view rerenders', function() {
run(function() {
view.set('context.foo', 'oof');
view.rerender();
});
equal(view.$().text(), 'OOF', 'should re-render if the parent view rerenders');
equal(view.$().text(), 'BORK', 'should not re-render if the parent view rerenders');
});

QUnit.module('ember-htmlbars: {{#unbound}} subexpression - helper form', {
Expand All @@ -195,7 +214,7 @@ QUnit.module('ember-htmlbars: {{#unbound}} subexpression - helper form', {

view = EmberView.create({
container,
template: compile('{{-capitalize (unbound -doublize foo)}}'),
template: compile('{{-capitalize (unbound (-doublize foo))}}'),
context: EmberObject.create({
foo: 'bork'
})
Expand Down Expand Up @@ -228,7 +247,7 @@ QUnit.test('it should re-render if the parent view rerenders', function() {
view.set('context.foo', 'oof');
view.rerender();
});
equal(view.$().text(), 'OOF OOF', 'should re-render if the parent view rerenders');
equal(view.$().text(), 'BORK BORK', 'should not re-render if the parent view rerenders');
});

QUnit.module('ember-htmlbars: {{#unbound boundHelper arg1 arg2... argN}} form: render unbound helper invocations', {
Expand Down Expand Up @@ -256,12 +275,12 @@ QUnit.module('ember-htmlbars: {{#unbound boundHelper arg1 arg2... argN}} form: r
}
this.set('value', value);
this.addObserver('value.firstName', this, this.recompute);
return get(value, 'firstName').toUpperCase();
return (value ? get(value, 'firstName').toUpperCase() : '');
}
}));

registry.register('helper:-fauxconcat', helper(function(params) {
return params.slice(0, -1).join('');
return params.join('');
}));

registry.register('helper:-concatNames', Helper.extend({
Expand All @@ -280,7 +299,7 @@ QUnit.module('ember-htmlbars: {{#unbound boundHelper arg1 arg2... argN}} form: r
this.set('value', value);
this.addObserver('value.firstName', this, this.recompute);
this.addObserver('value.lastName', this, this.recompute);
return get(value, 'firstName') + get(value, 'lastName');
return (value ? get(value, 'firstName') : '') + (value ? get(value, 'lastName') : '');
}
}));
},
Expand All @@ -304,41 +323,7 @@ QUnit.test('should be able to render an unbound helper invocation', function() {

view = EmberView.create({
container,
template: compile('{{unbound -repeat foo count=bar}} {{-repeat foo count=bar}} {{unbound -repeat foo count=2}} {{-repeat foo count=4}}'),
context: EmberObject.create({
foo: 'X',
numRepeatsBinding: 'bar',
bar: 5
})
});
runAppend(view);

equal(view.$().text(), 'XXXXX XXXXX XX XXXX', 'first render is correct');

run(function() {
set(view, 'context.bar', 1);
});

equal(view.$().text(), 'XXXXX X XX XXXX', 'only unbound bound options changed');
});

QUnit.test('should be able to render an unbound helper invocation with deprecated fooBinding [DEPRECATED]', function() {
registry.register('helper:-repeat', helper(function([value], {count}) {
var a = [];
while (a.length < count) {
a.push(value);
}
return a.join('');
}));

var template;
expectDeprecation(function() {
template = compile('{{unbound -repeat foo countBinding="bar"}} {{-repeat foo countBinding="bar"}} {{unbound -repeat foo count=2}} {{-repeat foo count=4}}');
}, /You're using legacy binding syntax/);

view = EmberView.create({
container,
template,
template: compile('{{unbound (-repeat foo count=bar)}} {{-repeat foo count=bar}} {{unbound (-repeat foo count=2)}} {{-repeat foo count=4}}'),
context: EmberObject.create({
foo: 'X',
numRepeatsBinding: 'bar',
Expand All @@ -359,7 +344,7 @@ QUnit.test('should be able to render an unbound helper invocation with deprecate
QUnit.test('should be able to render an bound helper invocation mixed with static values', function() {
view = EmberView.create({
container,
template: compile('{{unbound -surround prefix value "bar"}} {{-surround prefix value "bar"}} {{unbound -surround "bar" value suffix}} {{-surround "bar" value suffix}}'),
template: compile('{{unbound (-surround prefix value "bar")}} {{-surround prefix value "bar"}} {{unbound (-surround "bar" value suffix)}} {{-surround "bar" value suffix}}'),
context: EmberObject.create({
prefix: 'before',
value: 'core',
Expand All @@ -380,7 +365,7 @@ QUnit.test('should be able to render an bound helper invocation mixed with stati
QUnit.test('should be able to render unbound forms of multi-arg helpers', function() {
view = EmberView.create({
container,
template: compile('{{-capitalizeName person}} {{unbound -capitalizeName person}} {{-concatNames person}} {{unbound -concatNames person}}'),
template: compile('{{-fauxconcat foo bar bing}} {{unbound (-fauxconcat foo bar bing)}}'),
context: EmberObject.create({
foo: 'a',
bar: 'b',
Expand All @@ -400,7 +385,8 @@ QUnit.test('should be able to render unbound forms of multi-arg helpers', functi

QUnit.test('should be able to render an unbound helper invocation for helpers with dependent keys', function() {
view = EmberView.create({
template: compile('{{-capitalizeName person}} {{unbound -capitalizeName person}} {{-concatNames person}} {{unbound -concatNames person}}'),
container,
template: compile('{{-capitalizeName person}} {{unbound (-capitalizeName person)}} {{-concatNames person}} {{unbound (-concatNames person)}}'),
context: EmberObject.create({
person: EmberObject.create({
firstName: 'shooby',
Expand All @@ -424,7 +410,7 @@ QUnit.test('should be able to render an unbound helper invocation in #each helpe
container,
template: compile(
['{{#each people as |person|}}',
'{{-capitalize person.firstName}} {{unbound -capitalize person.firstName}}',
'{{-capitalize person.firstName}} {{unbound (-capitalize person.firstName)}}',
'{{/each}}'].join('')),
context: {
people: Ember.A([
Expand All @@ -451,7 +437,7 @@ QUnit.test('should be able to render an unbound helper invocation with bound has

view = EmberView.create({
container,
template: compile('{{-capitalizeName person}} {{unbound -capitalizeName person}} {{-concatNames person}} {{unbound -concatNames person}}'),
template: compile('{{-capitalizeName person}} {{unbound (-capitalizeName person)}} {{-concatNames person}} {{unbound (-concatNames person)}}'),
context: EmberObject.create({
person: EmberObject.create({
firstName: 'shooby',
Expand All @@ -473,14 +459,14 @@ QUnit.test('should be able to render an unbound helper invocation with bound has
QUnit.test('should be able to render bound form of a helper inside unbound form of same helper', function() {
view = EmberView.create({
template: compile(
['{{#unbound if foo}}',
['{{#if (unbound foo)}}',
'{{#if bar}}true{{/if}}',
'{{#unless bar}}false{{/unless}}',
'{{/unbound}}',
'{{#unbound unless notfoo}}',
'{{/if}}',
'{{#unless (unbound notfoo)}}',
'{{#if bar}}true{{/if}}',
'{{#unless bar}}false{{/unless}}',
'{{/unbound}}'].join('')),
'{{/unless}}'].join('')),
context: EmberObject.create({
foo: true,
notfoo: false,
Expand Down Expand Up @@ -519,7 +505,7 @@ QUnit.test('should lookup helpers in the container', function() {
}));

view = EmberView.create({
template: compile('{{unbound up-case displayText}}'),
template: compile('{{unbound (up-case displayText)}}'),
container,
context: {
displayText: 'such awesome'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ QUnit.test('should replace DOM representation if rerender() is called after elem
run(function() {
view = EmberView.extend({
rerender() {
this._super.apply(this, arguments);
this._super(...arguments);
}
}).create({
template: compile('Do not taunt happy fun {{unbound view.shape}}'),
Expand Down
Loading

0 comments on commit 4d10f6c

Please sign in to comment.