codepb/jquery-template

Question regarding data-template-bind

Closed this issue · 4 comments

elgs commented

@codepb, would you please take a look at this line:
https://github.com/codepb/jquery-template/blob/master/dist/jquery.loadTemplate-1.5.6.js#L474

With current code, the value is bound to the markup, so the value has to be a primitive type.
$this.attr(this.attribute, applyDataBindFormatters($this, value, this));

So, why not change attr to data, like so:
$this.data(this.attribute, applyDataBindFormatters($this, value, this));
so that the data is managed by jQuery internally, it could be any object or array, and is no longer limited to primitive types.

I've done a bit of reading, and I'm hesitant to do this. If I switch to use data then it is a breaking change (as accessing the values would now be different). Code that bound data, then accessed it using attr would no longer function on update.

I can see the merits of this approach. More thought needs to go into it to decide how to support complex types.

elgs commented

Thanks @codepb. I think I can do all things I need with afterInsert and complete now, with the following code:

afterInsert: function (elem, data) {
    elem.data('my_data', data);
},
complete: function () {
    e.preventDefault();
    $('#my_button').click(function (e) {
        var myData = $(this).parent().parent().data('my_data');
    }
}

I love it. :)

elgs commented

Now I am thinking, why not make them both (optionally) available in data and attr. They have different use cases. It's easier to directly use the values in the html with attr, whereas it's easier to get the data object in javascript code with data.

Can you open a new issue for that so I can mark it as an enhancement and have it clear what it is? I will close this issue again.