简体   繁体   中英

Connecting views in Durandal JS

I'm pretty new to Durandal so this may be obvious, but how do I chain logic between two viewmodels ?

So lets say I'm on a page that only has a list of items. I drill down on an item which loads the next page. Now in the activate method I would really like to do this next/previous logic...

activate: function(data, previousViewModel) {
    var Id = Number(data);
    // Load data

    var entityInTable = ko.utils.arrayFirst(previousViewModel.table.data(), function (item) {
        return item.Id() == Id;
    });
    var i = previousViewModel.table.data().indexOf(entityInTable);
    vm.previous = previousViewModel.table.data()[i - 1];
    vm.next = previousViewModel.table.data()[i + 1];
}

Is it possible/easy? Thanks.

Edit: Currently I am routing to this view by passing the view URL to the table control.

Table.prototype.openItem = function(item) {
    m_router.navigate(this.detailsUrl + "/" + item.Id());
}

Ideally I want to know which view I've been routed from when activating, so I can adjust the resulting views appearance and logic to direct it at that.

Yes it is both possible and fairly easy. You could either require() the other view model from the previous or you could simply 'new' up an instance of that view model as set a property on it. If you are doing a page change you can't really pass the view model into the next one, but if you are composing the view model you can.

If you elaborate on how one view model fires up the next I can elaborate further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM