moappi / json2html

Json2html is a lightning fast client side javascript HTML templating library with wrappers for both jQuery and Node.js.
https://www.json2html.com
MIT License
626 stars 104 forks source link

Can't add onerror attribute to img ... #16

Closed donShakespeare closed 8 years ago

donShakespeare commented 8 years ago

json2html will not allow onerror...

moappi commented 8 years ago

Firstly make sure you're using the jquery plugin for json2html https://github.com/moappi/jquery.json2html

The following code has been tested to work on the latest versions of Chrome & Firefox and Safari 5.1.7.

    var transform = {"tag":"img","onerror":function(){console.log('Unable to load image');},"src":"notworking.png"};

     $(function(){
         $('body').json2html({},transform);
    });

Note that the plugin will attach an event to the corresponding jquery event, for more details on the error event in jquery (and its limitations) check https://api.jquery.com/error/

donShakespeare commented 8 years ago

Thanks for the response and assurance. I did not realize there was a secondary jQuery plugin, I thought the version of json2html.js was the jQuery version.

For the record, with the jQuery plugin added...

    // document.getElementById('created').innerHTML = json2html.transform(data, transform);
    $('#created').json2html(data, transform);

the first line would not run onerror the second line works perfectly Question is how to pass in options. Till now I can't get the div#created content to be 'replaced'

Update I am using

 $('#created').empty().json2html(data, transform);
moappi commented 8 years ago

In order to leverage the jquery events for json2html you'll have to use the jquery plugin otherwise the events won't be registered. If you're looking to clear out the element "#created" first before inserting the transform then you can just use $('#created').html('').json2html(....)

donShakespeare commented 8 years ago

The people claim .empty is faster than .html('') http://stackoverflow.com/questions/17543711/jquery-difference-html-vs-empty

@moappi this your app is one of the most perfect things I have come across in a long while. I have successfully built a powerful backend Gallery with it...you have all my cheers

moappi commented 8 years ago

thanks for the tip! I'll start using .empty() instead. Also thanks for the kudos, feel free to post up the project if it's public facing, always great to see json2html in action