leonidas / transparency

Transparency is a semantic template engine for the browser. It maps JSON objects to DOM elements by id, class and data-bind attributes.
http://leonidas.github.com/transparency/
MIT License
969 stars 112 forks source link

How to avoid rendering text/html and only bind to an attribute? #102

Open matths opened 11 years ago

matths commented 11 years ago

So this might be not an issue, but maybe just not so well documented.

I would like to have a directive to fill stuff into an href="" attribute of an element, but not changing the text/html content of that html tag. In my example "read more…" should be left untouched. What's the way to do this?

<div class="template1">
    <a data-bind="moreLink" href="#">read more…</a>
</div>
$('.template1').render({moreLink: 'http://www.somethi.ng'}, {
    moreLink: {
        href: function () { return this.moreLink; }
    }
});
matths commented 11 years ago

I hate to answer to myself, but if I change the code to this, it works:

$('.template1').render({moreLinkXXX: 'http://www.somethi.ng'}, {
    moreLink: {
        href: function () { return this.moreLinkXXX; }
    }
});

But is there a way to do it without that change to either model keys or data-bind keys? Is there a way to return the former value inside the method of the directives?

fragoulis commented 11 years ago

In your example above if you log the arguments passed to the callbacks, the former/current value is indeed passed in arguments[0].value.

$('.template1').render({moreLinkXXX: 'http://www.somethi.ng'}, {
    moreLink: {
        href: function () { console.log(arguments); return this.moreLinkXXX; }
    }
});