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

Try to bind a variable to t-content? #17

Closed
pwFoo opened this issue May 18, 2019 · 14 comments
Closed

Try to bind a variable to t-content? #17

pwFoo opened this issue May 18, 2019 · 14 comments
Assignees

Comments

@pwFoo
Copy link

pwFoo commented May 18, 2019

Hi @anywhichway,

It works fine to set a string to t-content, but I tried to bind a variable as done inside of html elements with t-content.

I have a remote template, which loads fine after bug #15 was fixed.
Now I try to bind a variable instead of a text / html string to be flexible and easy update element content by ajax calls.

<div t-content="${myvar1}">template.html file content</div>

I see the initial html element content without t-content or values like t-content="my value" works fine and replaces the html content as should.
Just with variable like ${myvar1} is will be empty.

Is it supported to bind t-content to a variable?

@pwFoo
Copy link
Author

pwFoo commented May 18, 2019

Tested variables inside of the element content works fine by the way. I bind the view() to the parent of the remote template and the firstname variable is filled as should.

Looks like t-content not supports variables?

@anywhichway
Copy link
Owner

@pwFoo Change the content of the template.html file in the examples directory to the below. It seems to work fine.

`


This message area was loaded remotely by remote_template.html. You should see the first name twice.

${firstname}
`

@pwFoo
Copy link
Author

pwFoo commented May 18, 2019

Strange. In my test it doesn't work with a second "model", firstname works for me too.

<html lang="en">
<head>
	<!--<script src="https://unpkg.com/tlx/browser/tlx.min.js"></script>-->
	<script src="https://unpkg.com/tlx@1.0.41/browser/tlx.min.js"></script>
	<style>
	input:invalid {
  		box-shadow: 0 0 5px 1px red;
	}
	</style>
</head>
<body id="body">
<div>
You must be loading this file over an http server for this example to work
</div>
<div id="message"></div>
<hr />
<div class="test1" style="color:red;">${myvar}</div>
<hr />
<div id="name">First Name: <input id="firstName" name="firstname" value="${firstname}" protect placeholder="Enter a name"></div>
<hr />
<div>Below there should be a message that a load has failed.</div>
<div id="fail"></div>
<script>

const model = tlx.reactor({firstname:"joe"}),
	 model2 = tlx.reactor({myvar:"TemplateContentVariableValue"}),
		url = new URL("./template.html",window.location.href),
	missing = new URL("./missing.html",window.location.href);


tlx.view(document.getElementById("name"),{model,linkModel:true});

tlx.view(document.getElementById("body"),{model:model2,linkModel:true});

tlx.view(document.getElementById("message"),{model,template:url.href});
tlx.view(document.getElementById("fail"),{model,template:missing.href});


//tlx.view(document.getElementsByClassName("test1"),{model:model2,linkModel:true});
//tlx.view(document.getElementById("test2"),{model:model2,linkModel:true});
</script>
</body>
</html>

template

<div id="remotetemplate1">template.html file content<br />${firstname}</div>
<div t-content="${myvar}"></div>
<div t-content="${firstname}"></div>

First and third line works as should, Second is empty?

@pwFoo
Copy link
Author

pwFoo commented May 18, 2019

myvar works in main html file in that line as should!

<div class="test1" style="color:red;">${myvar}</div>

@pwFoo
Copy link
Author

pwFoo commented May 18, 2019

another example based on issue6.html (nodelist test).

<!DOCTYPE html>
<html lang="de">
  <head>
  <style>
	input:invalid {
  		box-shadow: 0 0 5px 1px red;
	}
	</style>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Titel der Seite | Name der Website</title>
    <script src="https://unpkg.com/tlx@1.0.41/browser/tlx.min.js"></script>
  </head>
  <body>
  	<div>Populate multiple fields with one call</div>
    <div class="fields" id="field1">Hello ${firstname}</div>
    <div class="fields" id="field2">Hello ${firstname}</div>
    <div class="fields" id="field3">Hello ${firstname} ${lastname}</div>
    <div>First Name: <input id="firstName" name="firstname" value="${firstname}" placeholder="Enter a name" protect></div>
    <div t-content="${firstname}"></div>

    <script type="text/javascript">
        const model = tlx.reactor();
        //tlx.view(document.getElementById("field1"),{model});
        //tlx.view(document.getElementById("field2"),{model});
        //tlx.view(document.getElementById("field3"),{model});
        //[].forEach.call(document.getElementsByClassName("fields"), function(elem) {
        //    tlx.view(elem,{model})
        //});
        tlx.view(document.getElementsByClassName("fields"),{model,linkModel:true});
        tlx.view(document.getElementById("firstName"),{model,linkModel:true});
    </script>
  </body>
</html>

Added a <div t-content="${firstname}"></div> and so it should update with the firstname value I think? I think t-content is similar to execute view() and so bind to a element?
The field stays empty in my test.

@anywhichway
Copy link
Owner

@pwFoo Acknowledging your issue. It will take a day or so to get to this.

@anywhichway anywhichway self-assigned this May 19, 2019
@anywhichway
Copy link
Owner

anywhichway commented May 23, 2019

@pwFoo This code works. Your example did not work because you were trying to replace variables in the body element with model2 data and variables using model inside a nested remote template inside an element contained in body.

<html lang="en">
<head>
	<!--<script src="https://unpkg.com/tlx/browser/tlx.min.js"></script>-->
	<script src="https://unpkg.com/tlx@1.0.41/browser/tlx.min.js"></script>
	<style>
	input:invalid {
  		box-shadow: 0 0 5px 1px red;
	}
	</style>
</head>
<body id="body">
<div>
You must be loading this file over an http server for this example to work
</div>
<div id="message"></div>
<hr />
<div id="test1" style="color:red;">${myvar}</div>
<hr />
<div id="name">First Name: <input id="firstName" name="firstname" value="${firstname}" protect placeholder="Enter a name"></div>
<hr />
<hr />
<div>Below there should be a message that a load has failed.</div>
<div id="fail"></div>
<script>

const model = tlx.reactor({firstname:"joe"}),
	 model2 = tlx.reactor({myvar:"TemplateContentVariableValue"}),
		url = new URL("./template.html",window.location.href),
	missing = new URL("./missing.html",window.location.href);


tlx.view(document.getElementById("name"),{model,linkModel:true});

tlx.view(document.getElementById("test1"),{model:model2,linkModel:true});

tlx.view(document.getElementById("message"),{model,template:url.href});
tlx.view(document.getElementById("fail"),{model,template:missing.href});


//tlx.view(document.getElementsByClassName("test1"),{model:model2,linkModel:true});
//tlx.view(document.getElementById("test2"),{model:model2,linkModel:true});
</script>
</body>
</html>

@pwFoo
Copy link
Author

pwFoo commented May 23, 2019

Hi @anywhichway, the html code of your example isn't displayed as source I think? Could you edit your post to show the html full source which should work?

So nested models down't work? Or remote template (elements) inside of a model?

Another question about multiple models and performance.
Is it better to work with different / many models or just one (big) / few models and variable updates?

@anywhichway
Copy link
Owner

@pwFoo Nested remote templates don't work. Never tested it. Will review code to see if it is possible to get working.

Better to use one model. Updates are only driven by the changes to specific properties on the model, so no more overhead than multiple models and easier to manage. That being said, if the domain warrants multiple models, using them is fine.

@pwFoo
Copy link
Author

pwFoo commented May 25, 2019

Ok, thanks! Good to know.

But username worked in remote template in my test. Just the second model wasn't replaced. I'll try it again, but haven't much time the next days. Would be nice to have it working in remote templates to update parts of the site.

@pwFoo
Copy link
Author

pwFoo commented Jun 6, 2019

t-content works fine with remote templates with my example and first model! Think it was a problem with the second model.

template:

<div style="border: 1px solid green">
    <div id="remotetemplate1">This is template.html file content</div>
    <hr />
    <div>Is name replaced here (inline variable)? "${firstname}"</div>
    <hr />
    <div>Variable replacement tested with "t-content" attribute:</div>
    <div t-content="${firstname}"></div>
</div>

Result:
image

Green border shows the content of rendered remote template with replaced variables. Output around comes from the loaded page itself with the model and tlx.

If I update firstname from developer tools all fields update as should. locale and remote loaded template contents.

@pwFoo
Copy link
Author

pwFoo commented Jun 6, 2019

It worked fine after I removed the second model!

@pwFoo
Copy link
Author

pwFoo commented Jun 6, 2019

Special case... if "initial content" (innerHtml of a html element) is set and a t-content attribute is added too. Would it possible to keep initial innerHtml content until t-content is NOT empty?

@anywhichway
Copy link
Owner

@pwFoo version 1.0.43 has been pushed. If the value of t-content is null or undefined then the original content will remain. If the value of t-content is anything else, including the empty string, the original content will be replaced.

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

No branches or pull requests

2 participants