【问题标题】:Durandal - custom modal click event binding errorDurandal - 自定义模式点击事件绑定错误
【发布时间】:2014-07-05 10:57:43
【问题描述】:

我在自定义模式和从模式视图模型映射点击事件时遇到问题。代码结构如下,首先我在 shell.js 中映射一个 headerView 像这样......

<div data-bind="compose:'viewmodels/header', preserveContext: true "></div>

然后在 header.js 中我需要一个单独的模块,通知......

Header.js

define(function (require) {
'use strict'

var ko = require('knockout'),
router = require('plugins/router'),
notification = require('models/notification');

var HeaderViewModel = function () {

var self = this;

self.title = ko.observable('');

self.toggleMenu = function () {
document.querySelector('nav').classList.toggle('selected');
};

self.setPage = function (target) {
document.querySelector('nav').classList.toggle('selected');
self.title(target);
//router.navigate('#' + target);
};

self.activate = function () {
notification.showMatch();
};

};

return new HeaderViewModel();
});

我的目标是激活函数中的 showMatch 函数,只是为了触发和测试模态。

notification.js

define(function (require) {

var Notification = function () {

var self = this;

var matcher = require('models/matcher'),
app = require('durandal/app'),
dialog = require('plugins/dialog'),
modalMatch = require('viewmodels/modalMatch');

self.showMatch = function () {

modalMatch.show().then(function(data) {
//callback function here (called after dialog is closed)
//also will return any passed data
alert('close test');
});

},


self.activate = function() {
this.showMatch();
}

};

return new Notification();
});

modalMatch.js

define(['plugins/dialog', 'plugins/router', 'knockout'], function (dialog, router, ko) {
"use strict";

var modalMatch = function (title) {
};

modalMatch.prototype.ok = function () {
dialog.close(this);
};

modalMatch.prototype.go = function (target) {
dialog.close(this);
alert(target);
//router.navigate('#' + target);
};

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

return new modalMatch();
});

modalMatch.html

<div class="modal ">
<div class="box">
<h3 class="title">Title</h3>

<div class="section">
<a href="#" class="selected">Test</a>
</div>
<div class="section">
<a  data-bind="click: $parent.go('home')" class="button chat">Home</a>
<a  data-bind="click: $parent.go('chat')" class="button sign">Chat</a>
</div>
</div>
</div>

这将启动模式并正确显示,但数据绑定点击不会响应应该提醒目标参数、关闭对话框并导航到目标的 go 函数调用。

控制台将记录以下错误 无法处理绑定点击:function (){return go('chat') } 消息:无法读取未定义的属性“关闭”; 视图:视图/modalMatch; ModuleId: viewmodels/modalMatch

我想我错过了设置中的一些细节,感谢任何帮助。

【问题讨论】:

  • 您的帖子标题不当。我们应该认真对待你吗?标题表明我们不应该这样做。
  • 我已在说明中使用正确的标题和代码示例更新了帖子。

标签: knockout.js modal-dialog durandal


【解决方案1】:

而不是

 <a  data-bind="click: $parent.go('home')" class="button chat">Home</a>

使用:

 data-bind="click: function(){ $root.go('home'); }

或更好:

 data-bind="click: function(){ $root.go($data); }

【讨论】:

  • 感谢您的回答。我根据建议更改了模态视图html中的代码。控制台现在不会在初始化加载期间警告任何错误,但单击 a-link 时没有任何反应。它不会触发 go 函数。它没有响应点击。
猜你喜欢
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多