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
Live-bound dynamic attributes don't get updated properly if they don't exist on initial execution #157
Comments
Does this happen in EJS too? |
Yes, but the solution for EJS looked like this:
What I think should be the equivalent in Mustache doesn't work the same though:
Should we just add a special case for |
I'll look into why this is happening, but in the meantime, here's a workaround:
|
It does work with your workaround, thanks ! |
Observe properties added after initialization do not live update properly in attributes
This ended up not being an issue with images specifically. From what I've narrowed it down to, can/view/render fails to properly set up live binding for observe properties if it is being set to a node's attribute (in this case src) if the property doesn't exist when the template is originally rendered. This occurs with both Mustache and EJS, so I added tests for both in the live-bind-observe-attr-157 branch. This works:
This doesn't:
|
I was hoping #153's fix would knock this one out too as they seemed very related. No such luck but hopefully the solution with that bug will help with this one. |
Observe properties added after initialization do not live update properly in attributes
What seems to be happening is that This works: // <img src="<%== this.attr("image") %>" alt="An image" />
var ___v1ew = [];
___v1ew.push("<img src=\"");
___v1ew.push(can.view.txt(0, 'img', 'src', this, function() {
return this.attr("image")
}));
___v1ew.push("\" alt=\"An image\" ", can.view.pending(), "/>");
___v1ew.push(can.view.txt(1, 'undefined', 0, this, function() {
return this.attr("image")
}));; So does this: // <img src="<% { %><%== this.attr("image") %><% } %>" alt="An image" />
var ___v1ew = [];
___v1ew.push("<img src=\"");
___v1ew.push(can.view.txt(0, 'img', 'src', this, function() {
var ___v1ew = []; {
___v1ew.push(can.view.txt(0, 'img', 'src', this, function() {
return this.attr("image")
}));
};
return ___v1ew.join('')
}));
___v1ew.push("\" alt=\"An image\" ", can.view.pending(), "/>");; But this doesn't: // <img <% { %>src="<%== this.attr("image") %>"<% } %> alt="An image" />
var ___v1ew = [];
___v1ew.push("<img ");
___v1ew.push(can.view.txt(0, 'img', 1, this, function() {
var ___v1ew = []; {
___v1ew.push("src=\"");
// The pending hookup this generates doesn't get created
// until after the can.view.pending() later on.
___v1ew.push(can.view.txt(0, 'img', 'src', this, function() {
return this.attr("image")
}));
___v1ew.push("\"");
};
return ___v1ew.join('')
}));
___v1ew.push(" alt=\"An image\" ", can.view.pending(), "/>");
___v1ew.push(can.view.txt(1, 'undefined', 0, this, function() {
return this.attr("image")
}));; |
After some more research, we need a way to ensure that the |
Renamed the title of this issue to better describe it and prevent more duplicates. |
So, I made my own test and imported andy's and this is working in 1.2: var template = can.view.mustache('<img {{#image}}src="{{.}}"{{/image}} alt="An image" />'),
data = new can.Map({
image: null
}),
url = "http://canjs.us/scripts/static/img/canjs_logo_yellow_small.png";
var frag = template(data),
img = frag.childNodes[0];
equal( img.hasAttribute('src'), false, "there is no src");
data.attr("image",url)
equal(img.hasAttribute('src'), true, 'Image should have src')
equal( img.src, url, "images src is correct" ); This passes. So I'm closing this issue? |
We can open another issue that allows you to write this |
This was what this issue is about. @andykant combined several similar issues into this one. |
…ttribute is empty see canjs/canjs#157
…_" when attribute is empty see canjs/canjs#157
…n attribute is empty see canjs/canjs#157
…when attribute is empty see canjs/canjs#157
…_" when attribute is empty see canjs/canjs#157
…__!!__" when attribute is empty see canjs/canjs#157
…when attribute is empty see canjs/canjs#157
…hen attribute is empty see canjs/canjs#157
…en attribute is empty see canjs/canjs#157
… attribute is empty see canjs/canjs#157
…when attribute is empty see canjs/canjs#157
Hello @andykant I was trying to use the img src as you mentioned in the workaround
This is my code what I have been trying to set the image src using a dataURI string to the template. But the src tag itself is not appended to the code after rendering the template. Can you help in this. Tried different combinations but didn't help. Working when the img src is a tag in my code.
|
This was discussed and fixed for EJS templates:
https://forum.javascriptmvc.com/topic/live-binding-an-image
But it doesn't work with Mustache templates. Check the following fiddle:
http://jsfiddle.net/donejs/4SmxJ/2/
It gives the !! error
The text was updated successfully, but these errors were encountered: