Created
July 15, 2014 21:42
-
-
Save anjiro/aa92039c9a40a708a936 to your computer and use it in GitHub Desktop.
jQuery.mmenu dynamic content bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script> | |
<script src="js/jquery.mmenu.min.all.js"></script> | |
<link rel="stylesheet" href="css/jquery.mmenu.all.css"> | |
<script> | |
//Automatically call a function to refresh the mmenu after the | |
// new data renders | |
ko.bindingHandlers.updateMenu = | |
{ | |
update:function(element,valueAccessor, allBindings, viewModel, bindingContext) | |
{ | |
var v = ko.utils.unwrapObservable(valueAccessor()); //get a dependency | |
if(v.length > 0) | |
bindingContext.$root.refreshMenu(); | |
} | |
}; | |
function ViewModel() | |
{ | |
var self = this; | |
self.menuData = ko.observableArray(['Data 1', 'Data 2', 'Data 3']); | |
self.val = 4; | |
self.addData = function() | |
{ | |
for(var i = 0; i < 10; i++) | |
self.menuData.push('Data ' + (i + self.val)); | |
self.val += i; | |
}; | |
self.refreshMenu = function() | |
{ | |
if(self.menuObject) | |
self.menuObject._init(); | |
} | |
} | |
$(document).ready( | |
function() | |
{ | |
var vm = new ViewModel(); | |
var m = $('#menu').mmenu({ offCanvas: false }); | |
vm.menuObject = m.data('mmenu'); | |
ko.applyBindings(vm); | |
} | |
); | |
</script> | |
</head> | |
<body> | |
<div> | |
<div style="height: 640px; margin-left: 440px;"> | |
Hello, this is content. | |
<button data-bind="click: addData">Push</button> | |
</div> | |
<nav id="menu"> | |
<ul> | |
<li><span>Static data</span> | |
<ul> | |
<li><a href="#">Data 1</a></li> | |
<li><a href="#">Data 2</a></li> | |
<li><a href="#">Data 3</a></li> | |
</ul> | |
</li> | |
<!-- This section is dynamically generated; initially, the content | |
is identical to the section above. --> | |
<li><span>Dynamic data</span> | |
<ul data-bind="foreach: menuData"> | |
<li><a href="#" data-bind="text: $data"></a></li> | |
</ul> | |
</li> | |
</nav> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment