Skip to content
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

[FEATURE] if helper inline form #9718

Merged
merged 1 commit into from
Nov 26, 2014
Merged

[FEATURE] if helper inline form #9718

merged 1 commit into from
Nov 26, 2014

Conversation

marcioj
Copy link
Contributor

@marcioj marcioj commented Nov 26, 2014

Adds a basic version of if helper in inline form. In the moment ids aren't support.
Bindings are supported, @mmun helped me to add it.
I added this behind a feature flag, not sure if it's needed, though.

@mixonic
Copy link
Member

mixonic commented Nov 26, 2014

Feature flag is absolutely needed! :-)

@marcioj marcioj force-pushed the mj-inline-if branch 3 times, most recently from a4c33ef to ee7bed3 Compare November 26, 2014 04:25
@mmun
Copy link
Member

mmun commented Nov 26, 2014

I discussed a bit with Kris and we decided to want a slightly different implementation of the ConditionalStream.

Basically, we should store this._lastCondition on the stream with initial value undefined. Inside of the valueFn we will check if this.condition.value() is different from this._lastCondition. If its different, we will subscribe to the new valueStream and unsubscribe from the old valueStream (except when this._lastCondition is undefined).

@mmun
Copy link
Member

mmun commented Nov 26, 2014

Here's what I had in mind (I changed the variable names to match the spider monkey API):

import Stream from "ember-metal/streams/stream";
import { read } from "ember-metal/streams/read";
import { create as o_create } from "ember-metal/platform";

function ConditionalStream(test, consequent, alternate) {
  this._super(conditionalValueFn);
  this.oldTest = undefined;
  this.test = test;
  this.consequent = consequent;
  this.alternate = alternate;
}

ConditionalStream.prototype = o_create(Stream.prototype);
ConditionalStream.prototype._super = Stream;

function conditionalValueFn() {
  var test = !!read(this.test);

  if (test !== this.oldTest) {
    if (this.oldTest === true) {
      this.consequent.unsubscribe(this.notify, this);
    }
    if (this.oldTest === false) {
      this.alternate.unsubscribe(this.notify, this);
    }
    if (test === true) {
      this.consequent.subscribe(this.notify, this);
    }
    if (test === false) {
      this.alternate.subscribe(this.notify, this);
    }
    this.oldTest = test;
  }

  return test ? read(this.consequent) : read(this.alternate);
}

export default ConditionalStream;

@marcioj
Copy link
Contributor Author

marcioj commented Nov 26, 2014

Hi @mmun, I updated the code based on your example

@mmun
Copy link
Member

mmun commented Nov 26, 2014

Thanks. I'm going to do a little touch up later.

var condition = params[0];
var truthy = params[1];
var falsy = params[2];
return new ConditionalStream(condition, truthy, falsy);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return value is not needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean? the return here is essential.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is a sub expression, not the if helper.

And return values now append a simple view: https://github.com/emberjs/ember.js/blob/master/packages/ember-htmlbars/lib/hooks/content.js#L13

New to me!

mmun added a commit that referenced this pull request Nov 26, 2014
[FEATURE] if helper inline form
@mmun mmun merged commit 436dae0 into emberjs:master Nov 26, 2014
@marcioj marcioj deleted the mj-inline-if branch November 27, 2014 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants