我新的ASP.NET MVC SPA和Knockout.js OS也許這是一個簡單的錯誤我犯了......與淘汰賽和ASP.NET MVC4 SPA多的ViewModels
情況:我有兩個partialviews在我的網站,我希望每個partialview都有他自己的Knockout ViewModel,所以我不會得到一個巨大的ViewModel。
我當前視圖模型:
/// <reference path="../_references.js" />
function MobileDeliveriesViewModel() {
var self = this;
// Data
self.currentDelivery = ko.observable();
self.nav = new NavHistory({
params: { view: 'deliveries', deliveryId: null }
});
// Test
self.foo = "FooBar"
self.bar = "BarFoo"
self.nav.initialize({ linkToUrl: true });
// Navigate Operations
self.showDeliveries = function() { self.nav.navigate({ view: 'deliveries' }) }
self.showCustomers = function() { self.nav.navigate({ view: 'customers' }) }
}
function BarFooViewModel() {
var self = this
//MobileDeliveriesViewModel.call(self)
self.bar2 = "BarFooTwo"
}
ko.applyBindings(new MobileDeliveriesViewModel());
ko.applyBindings(new MobileDeliveriesViewModel(), $('#BarFoo')[0]);
ko.applyBindings(new BarFooViewModel(), document.getElementById('BarFoo'));
我Index.cshtml:
<div data-bind="if: nav.params().view == 'deliveries'">
@Html.Partial("_DeliveriesList")
</div>
<div class="BarFoo" data-bind="if: nav.params().view == 'customers'">
@Html.Partial("_CustomersList")
</div>
<script src="~/Scripts/App/DeliveriesViewModel.js" type="text/javascript"></script>
我CustomerPartialView:
<div id="BarFoo" class="content">
<p data-bind="text: bar"></p>
<p data-bind="text: bar2"></p>
<button data-bind="click: showDeliveries, css: { active: nav.params().view == 'deliveries' }">Deliveries</button>
</div>
我DeliveriesPartialView:
<div class="content">
<p data-bind="text: foo"></p>
<button data-bind="click: showCustomers, css: { active: nav.params().view == 'customers' }">Customers</button>
</div>
如果我運行它,它不會承認的BAR2 propertie在我BarFooViewModel ...
我已經試過2個不同的applyBindings在我的ViewModel結束。
任何人有一個想法或是他們的更好的方式/模式來做到這一點?
謝謝,articel非常有用 – Renzzs