Reputation: 637
I am struggling with what I imagine is a really daft problem. I am iterating through a collection in Knockout.JS using 'foreach', and need to create a link for each item using variables within the collection.
The problem is that my url is generated on the fly using variables from the view model, and I need to combine these with variables in the Knockout collection.
Here is my block containing the Knockout loop.
<div data-bind="foreach: pagedList" class="span8" style="border-bottom: 1px solid #0094ff;">
<div style="cursor: pointer;">
<p data-bind="text: hotelId"></p>
<p data-bind="text: name"></p>
<p data-bind="text: hotelRating"></p>
<p data-bind="text: propertyCategory"></p>
</div>
</div>
Ideally I want to add the link to the parent div through the 'onclick="window.location=' method. I have tried using Action.Link and adding in the Knockout variables through string concatenation, as so;
<div style="cursor: pointer;" onclick="window.location="@Url.Action( "Index", "Hotel", new { regionId = Model.region.regionId, regionName = HttpUtility.UrlPathEncode(Model.region.regionNameLong.ToString().Replace(",","")).Replace("%20","-"), hotelId = " + hotelID() + ", hotelName = HttpUtility.UrlPathEncode(Convert.ToString(" + name() + ").Replace(",","")).Replace("%20","-") })"> </div>
But this gives an 'Object not instantiated error'. Secondly, I tried using Knockout first, through the 'data-bind="attr:' method, as so;
<a href="someurl" data-bind="attr: { href: '/Region/' + '@Model.region.regionId ' + '/' + '@HttpUtility.UrlPathEncode(Model.region.regionNameLong.ToString().Replace(",","")).Replace("%20","-") ' + '/' + hotelID() + '/' + '@HttpUtility.UrlPathEncode(Convert.ToString(" + name() + ").Replace(",","")).Replace("%20","-")' }, text: hotelId()"></a>
Again no dice.
I know this is mixing client side and server side paradigms, but can't think of another way without ditching Knockout.
Does anybody have any experience with this?
Upvotes: 3
Views: 4676
Reputation: 906
In the future it will really help if you create a JSFiddle of your problem.
Your second approach is closer.
Here is an example that uses a custom Knockout binding and template:
You can use razor to set values on the server side before you send the JavaScript to the client.
HTML is:
<div data-bind="template: { foreach: myItems, name: 'template-item' }" class="span8" style="border-bottom: 1px solid #0094ff;"></div>
<!-- basic template -->
<script type="text/html" id="template-item">
<p data-bind="text: hotelId"></p>
<p data-bind="text: hotelName"></p>
<p data-bind="text: hotelRating"></p>
<p data-bind="text: propertyCategory"></p>
<a data-bind="KoYourCustomBind: $data" href="#">link</a>
</script>
and JavaScript is:
// sample viewModel
var testViewModel = function(){
var self = this;
// another viewmodel property you want to use in url creation.
this.urlPart = "/ALLUSE/";
// array or list is called myItems
var iData = [new myItem("999", "Sheraton", "CAT7"), new myItem("007", "Marriott", "CAT11"), new myItem("212", "Budget Inn", "CAT00")];
this.myItems = ko.observableArray(iData);
}
var myItem = function(sHotelId, sName, sPropertyCategory) {
var self = this;
this.hotelId = sHotelId;
this.hotelName = sName;
this.hotelRating = 4; //hardcoded. only 4 star hotels here :)
this.propertyCategory = sPropertyCategory;
};
// ko bindings
ko.bindingHandlers.KoYourCustomBind = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext)
{
var item = valueAccessor();
var url = 'http://' + bindingContext.$parent.urlPart + item.hotelId + item.propertyCategory;
$(element).text(item.hotelName);
$(element).attr('href', url);
}
};
var vm = new testViewModel();
ko.applyBindings(vm);
The fiddle example depends on jQuery and Knockout.
Upvotes: 1