Durandal: the best way to transfer data between ViewModels

Durandal: What is the correct way to transfer data (parameters) between view modes, if possible (without accessing the backend).

let's say I have 2 submissions review and details . I want the user click in the list item from the overview to get the identifier of this element and pass it into the Viewmodel details so that I can start working with this id.

thank you for your help

+4
source share
1 answer

Hint: you probably need a route oriented approach. The other is for the sake of completeness.

Viewmodel oriented approach , : , . clickedElementId , ( , , ). , , () , , . , " ", , ( ).

, , , , . () . , , "details/: id" ( id, ) "details (/: id)" ( id, ).

:

overview.onElementClick = function (e) {
    var element = this, // The idea is that you need the clicked element for the next line of code
        koDataForElement = ko.dataFor(element);

    router.navigate('details/' + koDataForElement.id);
}

ko.dataFor - , viewmodel, , , . clicked . .

viewmodel :

details.activate = function (id) {
    // id is the parameter we defined for the route. Now you are free to leverage it inside your second view!
};

: : . , , - :

<div data-bind="foreach: myListOfElements">
    <a href="#" data-bind="attr: { href: '#details/' + id }">listElementGoesHere</a>
</div>

! , ,

+8

All Articles