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

Add for(integerVal) counting loops #709

Merged
merged 3 commits into from
Dec 23, 2019
Merged

Add for(integerVal) counting loops #709

merged 3 commits into from
Dec 23, 2019

Conversation

JaneOri
Copy link
Contributor

@JaneOri JaneOri commented Jul 31, 2019

Hey fam!

This adds integer count loops to output based on numbers.
Most frequently for me I've needed this for heavy visual builds.

	var template = stache("<div>{{#for(integerValue)}}[{{scope.index}}]{{/for}}</div>");
	var frag = template({
		integerValue: 3
	});
	// frag.firstChild.innerHTML === "[0][1][2]"

I queried about this years ago, wish I would have taken a look sooner since it was so easy to implement.

I understand if you decide against the feature, but I'd really like to have it and would be happy to discuss further if needed!

Hope you're all well!
//James

result.push(
options.fn( options.scope
.add({ index: i }, { special: true })
.addLetContext(variableScope) )
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we should create a variable?

{{# for(ittr of this.integerValue) }}
  <li>{{scope.index}}: {{ ittr }}</li>
{{/ for }}

I sorta think access should be only through scope.index like: {{# for( this.integerValue ) }} {{scope.index}} {{/for}}.

Copy link
Contributor

Choose a reason for hiding this comment

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

On second thought, I think it's ok to make {{# for(ittr of this.integerValue) }} work. But I wouldn't show this as the main way of using it.

@@ -14,6 +14,23 @@ var bindAndRead = function (value) {
}
};

function forOfInteger(integer, variableName, options) {
var result = [];
for (var i = 0; i < integer; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

it seems there's some indentation inconsistency

@@ -65,6 +65,32 @@
```
@codepen

If a positive integer value is passed, it will loop that number of times.
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably clean up the signature docs, specifically around this part:

EXPRESSION {KeyLookup Expression|Call Expression}: An expression that typically returns a list like data structure. If the value of the EXPRESSION is an observable list (for example: can-define/list/list), the resulting HTML is updated when the list changes. When a change in the list happens, only the minimum amount of DOM element changes occur. The list itself can also change, and a difference will be performed, which also will perform a minimal set of updates.

I think we should make it more clear what happens if the expression evaluates to:

  • An array-like structure (linking to canReflect.isListLike would be good here)
  • An object-like (key-value) data structure
  • A Number

@justinbmeyer
Copy link
Contributor

Yeah, I think this is a great feature! I added notes for a few cleanup items. Thanks @James0x57 !

@JaneOri
Copy link
Contributor Author

JaneOri commented Jul 31, 2019

Hey Justin! I've Addressed each item

Here's what the updated expression part in the docs looks like locally now:
image

@justinbmeyer justinbmeyer merged commit 1d6b736 into master Dec 23, 2019
@justinbmeyer justinbmeyer deleted the nLoops branch December 23, 2019 02:57
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.

None yet

2 participants