簡體   English   中英

Dust.js覆蓋渲染中的Knockout可觀察對象

[英]Dust.js overwrites Knockout observables on render

我在一個項目上一起使用Dust.jsKnockout.js ,使用一個稱為Duster-KO的模塊將兩者集成在一起。 當我嘗試渲染客戶端塵埃模板時會發生問題:當我將observable或任何包含可觀察對象的對象傳遞給Context參數中的dust.render()時,Dust實際上將KO observable設置為a “塊”對象。 我相信這是因為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)
  }
}

在上面的示例中,在渲染塵土模板之前,每個訪客的名字和姓氏都保持原樣,並且應該保持不變。 但是,之后將它們更改為塊對象。

而且,在有人提出建議之前,不幸的是,現在只能選擇除塵並僅使用淘汰賽。

您是否應用了Duster-Ko自述文件中提到的hack?

為什么除塵:(

那不愉快的事。

Dust希望所有功能標簽都接受一組參數(塊,上下文)。 我們可以為每個KO觀察者構建一個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= Chunk Tis all! Checkout lib/dust-patch.js for a patch 

針對未指定的Dust源(目前,dust-core-0.3.0.js是預期的目標)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM