binding to the src of an image

4,285 views
Skip to first unread message

JDeville

unread,
Jan 26, 2011, 4:45:21 PM1/26/11
to KnockoutJS
Is there an easy way to bind to the src attribute of an image? I
don't see a binding for it. I suppose I could create a template for
it, but that seems like overkill.

Does anyone have a generic binding that will just let me pick the name
of the attribute I want to set, and then set it? So far, the bindings
have felt kinda restrictive.

JDeville

unread,
Jan 26, 2011, 5:19:06 PM1/26/11
to KnockoutJS
I wrote a custom binding that is generic. It will set any attribute.
In my case, I'm using it to set the src of an image, so I'd use it
like this:

<img data-bind("attr: image_source, name:'src'")

ko.bindingHandlers.attr = {
update: (element, valueAccessor, allBindings) ->
$(element).attr(allBindings().name, valueAccessor())
}

If there's a better way to do that, please let me know. If not,
perhaps it could be included in a future release?

green

unread,
Jan 26, 2011, 6:06:27 PM1/26/11
to knock...@googlegroups.com
By using https://github.com/greenlaw110/knockout-ext, you could do this: <img data-bind="attr: {src: myimage}"></img>. E.g. http://jsfiddle.net/greenlaw110/uKseh/1/

JDeville

unread,
Jan 26, 2011, 9:10:54 PM1/26/11
to KnockoutJS
Nice! Thanks green!

On Jan 26, 6:06 pm, green <greenlaw...@gmail.com> wrote:
> By usinghttps://github.com/greenlaw110/knockout-ext, you could do this:
> <img data-bind="attr: {src: myimage}"></img>. E.g.http://jsfiddle.net/greenlaw110/uKseh/1/

Steven Sanderson

unread,
Jan 27, 2011, 3:30:39 AM1/27/11
to knockoutjs
Hey, that's really cool! Green, would you be happy for me to put your "attr" binding in KO core? It looks ideal.

Thanks
Steve

mcoolin

unread,
Jan 27, 2011, 6:44:28 AM1/27/11
to KnockoutJS
That would be a nice addition.

On Jan 27, 3:30 am, Steven Sanderson <st...@codeville.net> wrote:
> Hey, that's really cool! Green, would you be happy for me to put your "attr"
> binding in KO core? It looks ideal.
>
> Thanks
> Steve
>

green

unread,
Jan 27, 2011, 5:52:05 PM1/27/11
to knock...@googlegroups.com
Sure. Probably you also want to put event binding from KO-ext into KO core. 

TurboRSV

unread,
Sep 11, 2012, 7:00:04 AM9/11/12
to knock...@googlegroups.com
What if the value of 'myimage' is undefined at runtime?
Hoe can we manage this? I just came across this problem. Can the 'if' binding be put to work here? How?

Casey Corcoran

unread,
Sep 11, 2012, 10:21:52 AM9/11/12
to knock...@googlegroups.com
Looks like you've resurrected a really old post... Are you using the built in "attr" binding or the custom one mentioned in the thread?

I'll typically use "if" in a ko comment for a scenario like that:

<!-- ko if: myimage -->
<img data-bind="attr:{src:myimage}"/>
<!-- /ko -->

Casey Corcoran

Mark D'Arensbourg

unread,
Oct 10, 2012, 3:53:54 PM10/10/12
to knock...@googlegroups.com
It's hacky but you can save a few bytes by using //:0 as a placeholder src attribute on your img.
Reply all
Reply to author
Forward
0 new messages