aurelia - 双向绑定不适用于 Aurelia 自定义属性
问题描述
我正在尝试实施Jeremy Danyow 为脏检查提供的解决方案。我正在使用 TypeScript,所以我尽可能地调整了他的代码。出于某种原因,isDirty
我的 VM 上的标志没有更新。这是我所拥有的:
Agency.ts - (我的虚拟机)
export class Agency {
public isDirty : boolean = false;
public agency : IAgency = null;
...
}
Agency.html - (我的观点)
<template>
<require from="../resources/attributes/dirty"></require>
${isDirty} <== Always displays false
<form dirty.two-way="isDirty"> <== have also tried dirty.bind
<div class="input-group">
<label>Agency Name</label>
<input type="text" value.bind="agency.name" />
</div>
...
</form>
<div if.bind="isDirty">
<button>Save Changes</button>
<button>Undo Changes</button>
</div>
</template>
脏.ts - (属性)
import {bindable, bindingMode} from 'aurelia-framework';
export class DirtyCustomAttribute{
private view: any;
private bindings: any[] = [];
@bindable({defaultBindingMode: bindingMode.twoWay }) value: any;
static inject = [Element]
constructor(public element : HTMLElement) {}
created(view) {
this.view = view;
}
bind() {
this.bindings = this.view.bindings
.filter(b => b.mode === bindingMode.twoWay && this.element.contains(b.target));
let i = this.bindings.length;
let self = this;
while (i--) {
let binding = this.bindings[i];
binding.dirtyTrackedUpdateSource = binding.updateSource;
binding.updateSource = function (newValue) {
this.dirtyTrackedUpdateSource(newValue);
if (!self.value) {
self.value = true;
}
}
}
}
unbind() { // See code in referenced link }
}
因此,使用日志记录,我可以看到updateSource()
正在调用代码并且self.value
正在设置标志。但是,div
在我的视图中带有按钮的 永远不会显示,并且${isDirty}
模板始终显示为 false。看起来这dirty.two-way="isDirty"
不是应该的双向绑定。
我哪里错了?
更新
所以看起来它可能是由于组合,所以我将添加额外的代码来展示它是如何组合的。
外壳.ts
export class Shell
{
public agencies : IAgency[] = []; //loaded in activate
public tablist : any[] = [
{title: "Detail", vm: PLATFORM.moduleName("./agency") },
...
];
public selectedAgency : IAgency = null;
public selectedVM : any = this.tablist[0];
}
外壳.html
<template>
...
<compose view-model.bind="selectedVM.vm" model.bind="selectedAgency"></compose>
</template>
代理.ts(附加)
activate(model){
this.agency = model;
...
}
解决方案
推荐阅读
- c# - 如何按规则拆分数组块?
- java - 如何安装闭包编译器所需的Java?
- sql - 从 2 个连接的表合并到表
- php - 如何使用 Laravel 返回具有给定表结构的嵌套 JSON 格式
- gfs - Google 文件系统应用程序与客户端通信
- android - 无法设置状态栏透明
- react-native - 使用嵌套的 DrawerNavigator、TabNavigator 和 StackNavigator 进行深度链接
- asp.net - 如何从布局页面(不是典型的控制器/视图)中的表单显示 ModelState 验证失败结果?
- git - 如何在不影响我签出的工作文件夹的情况下从远程存储库重置我的整个 .git 文件夹?
- css - 如何不重复具有多个响应式背景的背景规范