Skip to content
This repository has been archived by the owner on Oct 24, 2021. It is now read-only.

Blaze outline #75

Merged
merged 5 commits into from
Nov 3, 2015
Merged

Blaze outline #75

merged 5 commits into from
Nov 3, 2015

Conversation

tmeasday
Copy link
Contributor


onCreated: function () {
this.foo = new ReactiveField(42);
}

And in the template:

{{instance.foo}}
Template.registerHelper('lookup', function (name) {
var computedField = new ComputedField(function () {
var value = Template.instance().view.lookup(name);
if (_.isFunction(value)) {
value = value();
}
return value;
}
return computedField();
});

But, autorun does not have access to template instance. :-( In this particular case this could be solved with:

Template.registerHelper('lookup', function (name) {
const view = Template.instance().view;
var computedField = new ComputedField(function () {
var value = view.lookup(name);
if (_.isFunction(value)) {
value = value();
}
return value;
}
return computedField();
});

The downside of this approach of course is that template helpers are always bound to data context, so this would rerun every time data context changes. Maybe to transition away from current Blaze, a new type of helpers could be defined? Like "methods"? Which would be the same as helpers, just that they have this set to the template instance and no automatic data context binding?

template.helpers({
...
}, {dataContext: false});

?

template.boundHelpers({
...
});
template.onCreated(function () {
this.foo = new ReactiveField(42);
});
template.events({
'click button': function (event, instance) {
instance.foo($(event.currentTarget).val());
}
});
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> Oh right, but I mean complicated state changes that shouldn't be in the event handler. Like
```js
Template.X.onCreated(function() {
this.updateFoo = (input) => {
const newValue = do * something + complicated / with * foo;
this.state.set('foo', newValue);
}
});
Template.X.events({
'click button': function(event, instance) {
instance.updateFoo(event.target.value);
}
});
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> :+1:
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> Exactly. That is what we even suggest in Blaze Components. That event maps should map between selectors and methods and then all event handling should be in methods. Then you can extend those methods in child classes. And then it is really cool when:

this.updateFoo = (input) => {
const newValue = do * something + complicated / with * this.foo();
this.foo(newValue);
}

So that you can access reactive fields as you would access some other method or property on template instance. This is a really powerful pattern.
- [x] <a href='#crh-comment-Pull d2231762773ac2dd73c8502c4004691d4c48def9 outlines/blaze.md 31'></a> <img src='http://www.codereviewhub.com/site/github-completed.png' height=16 width=60>&nbsp;<b><a href='https://github.com/meteor/guide/pull/75#discussion_r43545240'>File: outlines/blaze.md:L1-57</a></b>
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> Maybe document wait for subscription to finish pattern I have to use regularly:

onRendered(function ()
const instance = Template.instance();
this.autorun(function (computation) {
if (!instance.subscriptionsReady()) return;
computation.stop();
// Use 3rd party stuff to render now something.
});
});

- <a href='https://github.com/stubailo'><img border=0 src='https://avatars.githubusercontent.com/u/448783?v=3' height=16 width=16'></a> Yeah, this is an unfortunate pattern, we should add it here. lol
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> Talking about promises, this is one very common pattern I am using and I have not yet made a package for it. This wait for condition and stop autoruns.
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> I am using it a lot for inter-component communication: https://github.com/peerlibrary/meteor-blaze-components/issues/82 But I have not yet found a good name or API for it.
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> Good one, thanks @mitar
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> :+1:
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> (PS this is exactly the sort of kludge that I think should make it into the guide .. ;) )
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> :+
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> :+1:
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> If you help me find a good API for this, I can maybe make a package which wraps it up. Like `AutorunPromise`? :-) Or `Tracker.wait`? Like `Tracker.wait(condition, body)`?
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> Note to myself, improved:

onRendered(function ()
const instance = this;
this.autorun(function (computation) {
if (!instance.subscriptionsReady()) return;
computation.stop();
Tracker.nonreactive(function () {
// Use 3rd party stuff to render now something.
});
});
});

- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> Lol. Circa 2012: https://github.com/tmeasday/meteor-deps-extensions#await
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> :+1:
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> So, should I make a package only for this or what? :-)
BTW, I am starting to think that this could also be combined with promises. I will wrote more into the ticket about promises.
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> Well I think a `tracker-extensions` package is probably a pretty handy thing if stuff can be built without needing changes to core. Please take anything from my old deps-extension package that is still relevant if you do end up making the package, and let me know if you make the package.
- [x] <a href='#crh-comment-Pull d2231762773ac2dd73c8502c4004691d4c48def9 outlines/blaze.md 44'></a> <img src='http://www.codereviewhub.com/site/github-completed.png' height=16 width=60>&nbsp;<b><a href='https://github.com/meteor/guide/pull/75#discussion_r43545430'>File: outlines/blaze.md:L1-57</a></b>
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> And attributes how are they changed?
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> @mitar I'm not quite sure what you mean here?
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> So DOM element attributes. Things like `<div {{attrs}}>`. So we should explain how do they reactivelly change as well, no?
BTW, with Blaze Components there is also a nice pattern which emerged for attributes. Namely that you can have multiple sources contribute to the same dict which you then render out. Like a dict of CSS classes, and inline style, and then you render them at once out. Instead of patching them together in templates. With inheritance and mixins you can do this really well.
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> Then you can have a simple helper like:

Converts an array of style classes into a class attribute. It doesn't return anything

if the array is empty (or null) so that class attribute is not unnecessarily created.

class: (styleClassesArray) ->
if styleClassesArray?.length
class: styleClassesArray.join ' '

And just call `<foo {{class myClasses}}>` and it works great. :-)
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> I think they behave reactively like any other helper, no?
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> :+1:
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> Yes, we should maybe just explain a bit here more, and give some good-practice examples for those attribute-helpers.
- [x] <a href='#crh-comment-Pull c5b034e4dad8ba662d32f972bf41ba4cf80cae53 outlines/blaze.md 16'></a> <img src='http://www.codereviewhub.com/site/github-completed.png' height=16 width=60>&nbsp;<b><a href='https://github.com/meteor/guide/pull/75#discussion_r43568728'>File: outlines/blaze.md:L1-59</a></b>
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> I think we should still mention it, no matter what. People should know about them because they are often documented in old tutorials and Stack Overflow answers. Just ignoring that will make them hard to understand how to migrate to new stuff.
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> Well this isn't the full documentation of spacebars (or is it?).
I think in general, in the guide we mention things we think people should use. Like we aren't going to mention allow and deny really.
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> @stubailo what do you think about this?
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> > I think in general, in the guide we mention things we think people should use.
Yea, but I think that with proper use they are still useful. So maybe we should document proper use, instead of not documenting it at all. :-)
- <a href='https://github.com/stubailo'><img border=0 src='https://avatars.githubusercontent.com/u/448783?v=3' height=16 width=16'></a> I think it's similar to allow/deny - we should mention it in the same way perhaps.
- [x] <a href='#crh-comment-Pull c5b034e4dad8ba662d32f972bf41ba4cf80cae53 outlines/blaze.md 27'></a> <img src='http://www.codereviewhub.com/site/github-completed.png' height=16 width=60>&nbsp;<b><a href='https://github.com/meteor/guide/pull/75#discussion_r43568745'>File: outlines/blaze.md:L1-59</a></b>
- <a href='https://github.com/mitar'><img border=0 src='https://avatars.githubusercontent.com/u/585279?v=3' height=16 width=16'></a> Let's do `instance = Template.instance()` here as well.
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> Yeah, I'm inclined to agree. My only hesitancy would be that in all documentation ever you find on the web you'll see `function(event, template)` in event handlers. We are now saying you should write `function(event, instance)` I guess.
@stubailo what do you think?
- <a href='https://github.com/stubailo'><img border=0 src='https://avatars.githubusercontent.com/u/448783?v=3' height=16 width=16'></a> I think it's not a big deal. I'd obviously prefer `this`, but `instance` or `self` would be second best. The word `template` is over-used, who knows what it refers to.
- <a href='https://github.com/tmeasday'><img border=0 src='https://avatars.githubusercontent.com/u/132554?v=3' height=16 width=16'></a> I think we just need to be consistent across template access, event handlers and helpers. I think `instance` works best for those three so we'll run with it.


<a href='https://www.codereviewhub.com/meteor/guide/pull/75?mark_as_completed=1'><img src='http://www.codereviewhub.com/site/github-mark-as-completed.png' height=26></a>&nbsp;<a href='https://www.codereviewhub.com/meteor/guide/pull/75?approve=1'><img src='http://www.codereviewhub.com/site/github-approve.png' height=26></a>&nbsp;<a href='https://github.com/meteor/guide/pull/75'><img src='http://www.codereviewhub.com/site/github-refresh.png' height=26></a>
<a href='#crh-end'></a>

@@ -0,0 +1,56 @@
# Blaze

1. Introduction to Blaze -- the tracker-backed handlebars like templating syntax
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically, the syntax is called spacebars, and the runtime is blaze. Like jsx and react.

Copy link
Contributor

Choose a reason for hiding this comment

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

Speaking of Tracker, where do we cover that in the guide??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. No, we don't explicitly. I guess it is bigger than Blaze (you should know about how it works in the React world too). Does it deserve it's own (shorter) article?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#77

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

1. `{{#if/unless}}`
2. `{{#each .. in ..}}`
3. `{{#let}}`
1. NOTE: we need to ensure that issues around lexical scope and event handlers are resolved before we fail to even mention `{{#each}}` and `{{#with}}`. But we should attempt it.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should still mention it, no matter what. People should know about them because they are often documented in old tutorials and Stack Overflow answers. Just ignoring that will make them hard to understand how to migrate to new stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well this isn't the full documentation of spacebars (or is it?).

I think in general, in the guide we mention things we think people should use. Like we aren't going to mention allow and deny really.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stubailo what do you think about this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think in general, in the guide we mention things we think people should use.

Yea, but I think that with proper use they are still useful. So maybe we should document proper use, instead of not documenting it at all. :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's similar to allow/deny - we should mention it in the same way perhaps.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

tmeasday pushed a commit that referenced this pull request Nov 3, 2015
@tmeasday tmeasday merged commit 1770b50 into master Nov 3, 2015
@tmeasday tmeasday deleted the blaze-outline branch December 7, 2015 06:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants