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.
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.
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;
});
<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>
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;
});
<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>
define(function () {
var ctor = function () {
this.title = 'I\'m an existing view';
};
return ctor;
});
<p data-bind="text: title"></p>
Live version available at http://dfiddle.github.io/dFiddle-2.0/#so/21821997. Feel free to fork.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With