javascript - Conflict between masonryjs and mustachejs -
i have syntax error somewhere in javascript seems slipping by. can either access content through file mustache template, or can hard code items (images , audio) , have items smoothly fill page masonryjs. when try add both, either or both not work. here codepen
what expect happen have page generated document feeding mustache template. should stack smoothly sites pinterest images tile neatly since used masonryjs.
here code works masonry
html
<div id="content" class="container clearfix"> <div class="item"> <img src="http://assets.wtfisthat.info/img/1.jpg" alt="" /> <audio controls> <source src="http://assets.wtfisthat.info/audio/1.wav"> </audio> </div> <div class="item"> <img src="http://assets.wtfisthat.info/img/2.jpg" alt="" /> <audio controls> <source src="http://assets.wtfisthat.info/audio/2.wav"> </audio> </div> <div class="item"> <img src="http://assets.wtfisthat.info/img/3.jpg" alt="" /> <audio controls> <source src="http://assets.wtfisthat.info/audio/3.wav"> </audio> </div> <div class="item"> <img src="http://assets.wtfisthat.info/img/4.jpg" alt="" /> <audio controls> <source src="http://assets.wtfisthat.info/audio/4.wav"> </audio> </div> <!-- rest of items handcoded --> </div> <!--/ #container -->
js
$(document).ready(function() { // initialize masonry $('#content').masonry({ columnwidth: 320, itemselector: '.item', isfitwidth: true, isanimated: !modernizr.csstransitions }).imagesloaded(function() { $(this).masonry('reload'); }); });
but when add mustache not hand coded, fails. tried doing
html
<div id="content" class="container clearfix"> <div id="image-list"></div> <script id="imgtpl" type="text/template"> {{#images}} <div class="item"> <img src="http://assets.wtfisthat.info/img/{{img}}" alt="{{alt}}"> <audio controls> <source src="http://assets.wtfisthat.info/audio/{{id}}.wav"> </audio> </div> {{/images}} </script> </div> <!--/ #container -->
js
// streams in data json file $(function() { $.getjson('https://s3-us-west-2.amazonaws.com/s.cdpn.io/101702/img.js', function(data) { var template = $('#imgtpl').html(); var html = mustache.to_html(template, data); $('#image-list').html(html); }); }); $(document).ready(function() { // initialize masonry $('#content').masonry({ columnwidth: 320, itemselector: '.item', isfitwidth: true, isanimated: !modernizr.csstransitions }).imagesloaded(function() { $(this).masonry('reload'); }); });
solved here: http://codepen.io/anon/pen/mwjxpw
i've changed code new elements appended existing ones, instead of pushing them inside own div.
then, should use masonry('appended', $elements)
once elements added, explained in page: adding items.
here modified script:
$.getjson('https://s3-us-west-2.amazonaws.com/s.cdpn.io/101702/img.js', function(data) { var template = $('#imgtpl').html(); var html = mustache.to_html(template, data); var $html = $(html) $('#content').append($html).masonry( 'appended', $html); });
Comments
Post a Comment