Durandal 2.0 custom dialog

北慕城南 提交于 2019-12-19 03:20:49

问题


I wish to make a Durandal custom dialog that adds a window frame with title and footer around an existing composable viewmodel.

I have made a customModal.html template

<div class="messageBox">
    <div class="modal-header">
        <h3 data-bind="text: title"></h3>
    </div>
    <div class="modal-body">
        <!--ko compose: { model: model, template: view }-->
        <!--/ko-->
    </div>
    <div class="modal-footer" data-bind="foreach: options">
        <button class="btn" data-bind="click: function () { $parent.selectOption($data); }, text: $data, css: { 'btn-primary': $index() == 0, autofocus: $index() == 0 }"></button>
    </div>
</div>

As you can see I wish to compose a viewmodel within the body of the customModal template. This is so that the viewmodel passed in is not tied to a modal display but can easily use one.

I have made a customModal.js model like this:

define(['plugins/dialog'], function (dialog) {
    var CustomModal = function (title, model, view, options) {
        this.title = title;
        this.model = model;
        this.view = view;
        this.options = options;
    };

    CustomModal.prototype.selectOption = function (dialogResult) {
        dialog.close(this, dialogResult);
    };

    return CustomModal;
});

But when I try and use it the compose binding searches for the template '.html' and fails.

Am I missing something here? And is this really the best way to do this?

Thanks.


回答1:


I created a simplified example that you can use as starting point. It consists of an index view/vm that optionally opens an existing view/vm in a customModal. The existing view model is returned on close so that it's accessible.

index.js

define(function(require){
    "use strict";

    var app = require('durandal/app'),
        CustomDialog = require('./customModal'),
        Existing = require('./existingView'),
        ctor;

    ctor = function(){
        this.dialog;
    };

    ctor.prototype.showCustomModal = function(){
        this.dialog = new CustomDialog('My title', new Existing());

        this.dialog.show().then(function(response){
            app.showMessage('Model title "' + response.title + '".');
        });
    };

    return ctor;

});

index.html

<div>
    <h3>Durandal 2.0 custom dialog</h3>
    <a href="#" data-bind="click: $data.showCustomModal">click here </a> to open an existing view model in a custom
    dialog.
</div>

customModal.js

define(['plugins/dialog'], function (dialog) {
    var CustomModal = function (title, model) {
        this.title = title;
        this.model = model;
    };

    CustomModal.prototype.ok = function() {
         dialog.close(this, this.model);
     };

    CustomModal.prototype.show = function(){
       return dialog.show(this);
    };

    return CustomModal;
});

customModal.html

<div class="messageBox">
    <div class="modal-header">
        <h3 data-bind="text: title"></h3>
    </div>
    <div class="modal-body">
        <!--ko compose: $data.model-->
        <!--/ko-->
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" data-bind="click: ok">Ok</button>
    </div>
</div>

existingView.js

define(function () {

    var ctor = function () {
        this.title = 'I\'m an existing view';
    };

    return ctor;
});

existingView.html

<p data-bind="text: title"></p>

Live version available at http://dfiddle.github.io/dFiddle-2.0/#so/21821997. Feel free to fork.



来源:https://stackoverflow.com/questions/21821997/durandal-2-0-custom-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!