Luther Baker
unread,Mar 3, 2012, 2:25:34 AM3/3/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to KnockoutJS
I am looping over groceries ... and trying to bind "shop()" to
"click". Unfortunately, knockout can't find the 'shop' function. Am I
doing something wrong?
It works on a root level button - but doesn't work within a knockout
foreach loop.
Thanks,
-Luther
----
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: shop is not defined;
Bindings value: text: name, click: shop
----
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<script src="/public/javascripts/jquery-1.6.4.min.js"
type="text/javascript" charset="utf-8"></script>
<script src="/public/javascripts/knockout-2.0.0.js" type="text/
javascript" charset="utf-8"></script>
<script src="/public/javascripts/knockout.mapping-
latest.debug.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" language="javascript">
jQuery(function($){
var viewModel = {
shop : function() {
alert('go shopping');
},
groceries: [
{ name: 'apple' },
{ name: 'eggs' },
{ name: 'butter' }
]
};
ko.applyBindings(viewModel);
});
</script>
</head>
<body>
<!-- WORKS -->
<button data-bind="click: shop">Shop</button>
<!-- throws ERROR in CONSOLE -->
<ul data-bind="foreach: groceries">
<li><div data-bind="text: name, click: shop" class="link"></div></
li>
</ul>
</body>
</html>