灰尘.js覆盖渲染上的挖空可观察量
Dust.js overwrites Knockout observables on render
我在一个项目中同时使用Dust.js和Knockout.js,使用一个名为Duster-KO的模块将两者集成在一起。当我尝试在客户端渲染 dust 模板时,就会出现问题:当我在 Context 参数中将可观察量或任何包含可观察量的对象传递给 dust.render() 时,Dust 实际上是将 KO 可观察量设置为"块"对象。我相信这是因为 Knockout 可观察量是函数,所以 Dust 认为我传递给它的函数是一个回调而不是可观察量,然后它正在执行并以某种方式以这种方式设置可观察量。
有没有办法避免这个问题,或者防止灰尘接触可观察物?
以下是我遇到的一个情况示例:
var guest = exports.guest = function(opts) {
this.first = ko.observable(opts.first||"")
this.last = ko.observable(opts.last||"")
// ... more model code here
}
var table = exports.table = function(opts) {
// This is an observable array of guest objects
this.guests = ko.observableArray(opts.guests||[])
this.template = "tableTemplate"
this.target = opts.target // This is whatever DOM element we are injecting the template into
// ... more model code here
var self = this
this.draw = function() {
// Before we render the Dust template, the guest's first and last name are as they should be
// this.ctx is a Context object inherited from another parent object, which has the current object pushed onto it
var rendered = dust.render(self.template, this.ctx)
// At this point in the code, the guest's first and last name have been set to Chunk objects, rather than their actual first and last names
self.target.appendChild(rendered)
}
}
在上面的示例中,在我渲染灰尘模板之前,每个客人的名字和姓氏都完好无损,并且应该如此。但是,之后它们将更改为 Chunk 对象。
不幸的是,在有人建议之前,删除灰尘并仅使用淘汰赛现在不是一种选择。
您是否应用了Duster-Ko自述文件中提到的黑客???
为什么尘埃黑客:(
不愉快的生意,那个。
Dust期望任何功能标签接受一组参数(块, 上下文)。我们可以为每个KO构建一个防尘包装器 观察者,并从中构建 Dust 上下文,但这似乎是一个 非常多不必要的对象创建。
相反,我们只是破解 Dust 以不像正常情况那样评估观察者 会,并以更多的库存标准处理善后 帮助程序筛选器。
出处
这些更改是在您使用的任何 dust*js 中完成的。
主要黑客:
Chunk.prototype.reference = function(elem, context, auto, filters) { - if (typeof elem === "function") { + if (typeof elem === "function" && elem.name != "observable") { elem = elem(this, context, null, {auto: auto, filters: filters});`
哦,还有,我们正在手动调用一些 Dust 模板和咳嗽评估 那。要手动调用模板,我们需要传入一个 Dust Chunk 对象,通常我们不会接触到它,所以:
+dust.chunk= Chunk Tis all! Checkout lib/dust-patch.js for a patch
针对未指定的尘源(目前,尘芯-0.3.0.js是 预期目标)。
- 将js对象更改为使用嵌套的可观察数组敲除js视图模型
- $scope$观察数组中的一个特定对象——Angular JS控制器
- 如何在Grunt.js中观察多个文件,但只在更改的文件上运行任务
- 无法观察Ember.js中的对象数组
- 在编辑记录上可观察的挖空 JS 计算
- 培根.js Bus.plug: 未捕获 错误: 不是可观察的 : [对象对象]
- 如何在 Knockout.js 中更新可观察数组后立即使用 DOM 容器
- Knockout.js:如何根据可观察数组中的更改更改值
- 使用挖空.js如何刷新 Select 元素中的可观察数组
- 灰尘.js覆盖渲染上的挖空可观察量
- 如何观察在 Sails .js 中添加到模型中的新记录
- 被动.js模式间接观察者报告相同的值
- Knockout.js性能-有多少可观察性
- Knockout JS未将所有成员设置为可观察
- 在ko.applyBindings(..)中执行Knockout js订阅函数(用于可观察对象)
- Knockout.js用单个json对象绑定一个可观察对象
- 淘汰js可观察扩展的执行顺序是什么
- Knockout.js:在可选定义的值上计算可观察性
- 去掉js可观察数组和复选框,选中绑定失败
- 聚合物是如何观察js工作的