Durandal Composition with can with deactivDeactivate

时间:2015-04-28 18:12:29

标签: durandal durandal-2.0

我正在使用Durandal 2.1,我的视图合成有问题。我有管理许多类型的项目的观点。我还想要一个视图来管理这些类型的子集。所以我创建了一个管理视图和一个manageubset视图。 manageubset视图只构成管理视图,并将包含项子集的数组传递给它。这样,用户可以转到/ 100 / manage或100 / managesubset,其中manageubset将仅允许用户管理项目的子集。我正在使用这种模式,因为我将有多个不同版本的managesubset。

我的问题是,当转到manageubset时,不会触发canDeactivate方法。无论如何,在编写时会激活canDeactivate和Deactivate生命周期事件吗?

根据Activator Lifecycle Callbacks here下的#3,我应该可以做到这一点,但我找不到任何好的例子。

代码:

manage.js

define(['durandal/app', 'plugins/router'], function (app, router) {
    var constructor = function () {
        var self = this;
        //...variable creation and assignment

        //life cycle events
        self.activate = function (viewmodel) {
            self.recordId(viewmodel.recordId);
            self.assignableTypes(viewmodel.assignableTypes);
            self.pageHeaderTitle = viewmodel.pageHeaderTitle;
            self.pageHeaderIcon = viewmodel.pageHeaderIcon;
        };

        self.canActivate = function (id) {
            var deferred = $.Deferred();

            //check if user has access to manage equipment

        };

        self.canDeactivate = function () {
            if (!self.saveSuccessfull() && this.isDirty()) {
                return app.showMessage("You have unsaved changes, are you sure you want to leave?", "Unsaved Changes", ["Yes", "No"]);
            }
            else {
                return true;
            }
        }
    };

    return constructor;
});

managesubset.js

 define([], function () {
    var recordId = ko.observable();
    var manageRecord = ko.observable();

    return {
        recordId: recordId,
        manageRecord: manageRecord,
        activate: function (id) {
            recordId(id);
            manageRecord({
                pageHeaderTitle: 'Manage Subset',
                pageHeaderIcon: 'cb-subset',
                assignableTypes: [102],
                recordId: recordId()
            });
        },
        canActivate: function (id) {
            var deferred = $.Deferred();

            //check if user has access to manage equipment
        }
    }
});

managesubset.html

 <div data-bind="compose: { model: 'manage', activationData: manageRecord() }"></div>

每次都正确调用激活。 deactivate和canDeactive是不起作用的,永远不会被调用。

0 个答案:

没有答案