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

Changing context in a Stache template breaks the data helper #1619

Closed
dylanrtt opened this Issue Apr 13, 2015 · 5 comments

Comments

Projects
None yet
5 participants
@dylanrtt
Contributor

dylanrtt commented Apr 13, 2015

In the migration from EJS to Mustache, we lost the ability to explicitly declare which data was used with the data helper. For example, in EJS you could use:

<div (el) -> can.data(el, 'todo', todo)></div>

In some cases, we rewrote the JS to expect the data to be the entire context, but in others, we worked around this limitation by changing the context in the template surrounding the usage of the data helper. For example:

<div {{#todo}} {{ data "todo" }} {{/todo}}></div>

Unfortunately, while upgrading to Stache, this broke due to an apparent bug that seems to put JS code into the DOM as exhibited by this fiddle.

http://jsfiddle.net/qYdwR/2476/

So far, this seems to only be a problem with {{#key}}{/key}} and not with {{#each key}}{{/each}} which also changes context. A similar issue also occurs with using #if to wrap around the data helper instead.

@daffl daffl added this to the 2.2.6 milestone Apr 22, 2015

@daffl

This comment has been minimized.

Show comment
Hide comment
@daffl

daffl Apr 22, 2015

Contributor

The result is indeed odd but I think the easiest solution for now is to pass the context you want to store in the data as the second argument:

<div id="test" {{ data "todo" todo }}></div>
$('#test').data('todo') // -> todo
Contributor

daffl commented Apr 22, 2015

The result is indeed odd but I think the easiest solution for now is to pass the context you want to store in the data as the second argument:

<div id="test" {{ data "todo" todo }}></div>
$('#test').data('todo') // -> todo
@dylanrtt

This comment has been minimized.

Show comment
Hide comment
@dylanrtt

dylanrtt Apr 22, 2015

Contributor

Oh, I didn't realize that was supported. I could have sworn I tested this before and that it did not work... but it seems to be working fine now.

You should add that to the docs so people know it's available.

Edit: The only possible issue I can see is that if you do {{ data "todo" todo.prop }}, the data will actually store a compute instead of the value of todo.prop.

Contributor

dylanrtt commented Apr 22, 2015

Oh, I didn't realize that was supported. I could have sworn I tested this before and that it did not work... but it seems to be working fine now.

You should add that to the docs so people know it's available.

Edit: The only possible issue I can see is that if you do {{ data "todo" todo.prop }}, the data will actually store a compute instead of the value of todo.prop.

marshallswain added a commit to marshallswain/canjs that referenced this issue Apr 23, 2015

Document setting context of Stache data helper
As mentioned in #1619, this documents setting the context of the data helper explicitly.

Does the "optional argument" syntax work in that @Signature?

@daffl daffl removed this from the 2.2.6 milestone May 13, 2015

@daffl daffl added the bug label May 13, 2015

@justinbmeyer

This comment has been minimized.

Show comment
Hide comment
@justinbmeyer

justinbmeyer May 14, 2015

Contributor

@daffl Bug? Or docs?

Contributor

justinbmeyer commented May 14, 2015

@daffl Bug? Or docs?

@daffl

This comment has been minimized.

Show comment
Hide comment
@daffl

daffl May 14, 2015

Contributor

I would say docs for this and @marshallswain already started that in #1640.

Contributor

daffl commented May 14, 2015

I would say docs for this and @marshallswain already started that in #1640.

@alexisabril alexisabril removed the bug label Sep 14, 2015

@alexisabril alexisabril added this to the 2.3.0 milestone Sep 14, 2015

@alexisabril

This comment has been minimized.

Show comment
Hide comment
@alexisabril

alexisabril Sep 15, 2015

Contributor

Closed by b7c8769

Contributor

alexisabril commented Sep 15, 2015

Closed by b7c8769

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment