0

我正在尝试在 Durandal 中设置 SPA,我需要将 JavaScript 对象从一个视图传递到另一个视图。我读过关于亲子关系的文章,我创建了一个(或者至少我认为我做了......)但由于某种原因它不起作用......我花了几个小时试图找到解决方案,但就我而言担心我这样做是对的。这是代码:

第一种观点:

<h3>List of candidates:</h3>
<table border="1" class="fixed">
    <col width="150" />
    <col width="150" />
    <col width="150" />
    <col width="150" />
    <thead>
        <tr>
            <th>Name</th>
            <th>Address</th>
            <th>Position Applied</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: candidates">
        <tr>
            <td data-bind="text: name"></td>
            <td data-bind="text: address"></td>
            <td data-bind="text: positionApplied"></td>
            <td align="center"><a href="#" data-bind="click: $parent.viewCandidate">View candidate</a>
            <a href="#" data-bind="click: $parent.removeCandidate">Remove candidate</a>
            </td>
        </tr>
    </tbody>
</table>

和它的JavaScript:

define(['durandal/app', 'knockout', 'canView'] , function (app, ko, cv) {


        viewCandidate: function () {

            cv.CandidateContainer(this.name, this.address, this.positionApplied);
            console.log(cv.container.name);
            document.location="#CandidateView";
        }, 

        removeCandidate: function() {
            CATObject.candidates.remove(this);
        },



    });

    return CATObject;

});

现在是我尝试检索它的第二页:

<p><strong>Name: &nbsp</strong><span data-bind="text: container.name"></span></p>
<p><strong>Address: &nbsp</strong><span data-bind="text: container.address"></span></p> 
<p><strong>Position applied: &nbsp</strong><span data-bind="text:container.positionApplied"></span></p>

和它的JavaScript:

define(['durandal/app', 'knockout'] , function (app, ko) {


    var viewCandObject = {
        displayName: 'Candidate view',

        container: {},

        CandidateContainer: function(name, address, positionApplied) {
            viewCandObject.container.name=name;
            viewCandObject.container.address=address;
            viewCandObject.container.positionApplied=positionApplied
            console.log("CandidateContainer ran");

            viewCandObject.testContainer();

        },

    }
    return viewCandObject;

});

任何帮助或提示将不胜感激。

4

0 回答 0