调试aurelia ViewModel类似于ko.toJson [英] debug Aurelia ViewModel similar to ko.toJson
本文介绍了调试aurelia ViewModel类似于ko.toJson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在基因敲除js中,您可以以不错的json格式输出ViewModel进行调试
in knockoutjs you can output the ViewModel in a nice json format for debugging
<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
如果有一种方法可以在奥雷利亚(Aurelia)做到这一点
if there is a way to accomplish the same in Aurelia
推荐答案
您可以创建一个自定义元素.
You could create a custom element.
以下是示例: https://gist.run?id=9eea8902521f4523ee2c
app.html
<template>
<require from="./debug"></require>
<input value.bind="firstName">
<input value.bind="lastName">
<debug></debug>
</template>
app.js
export class App {
firstName = 'Donald';
lastName = 'Draper';
}
debug.html
<template>
<pre><code>${json}</code></pre>
</template>
debug.js
export class Debug {
bindingContext = null;
updateJson() {
if (this.bindingContext === null) {
this.json = 'null';
} else if (this.bindingContext === undefined) {
this.json = 'undefined'
} else {
// todo: use a stringify function that can handle circular references.
this.json = JSON.stringify(this.bindingContext, null, 2);
}
}
bind(bindingContext) {
this.bindingContext = bindingContext;
this.updateJson();
this.interval = setInterval(::this.updateJson, 150);
}
unbind() {
this.bindingContext = null;
clearInterval(this.interval);
}
}
结果
这篇关于调试aurelia ViewModel类似于ko.toJson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文