2013-01-22 31 views
0

我有一個使用qx.data.marshal.Json.createModel創建的模型,它包含一個名爲「startDate」的字段,它是一個Date實例。我想重寫getStartDate以便返回模型中Date和Date不同的副本。如何將自定義方法添加到使用qx.data.marshal.Json.createModel創建的模型中?

我見過http://manual.qooxdoo.org/2.1/pages/data_binding/models.html但我只找到了「查看qx.data.store.IStoreDelegate的API文檔以查看可用的方法以及如何實現它們。」。 IStoreDelegate信息不完整,我不知道如何使用它(http://demo.qooxdoo.org/2.1/apiviewer/index.html#qx.data.store.IStoreDelegate)。

var delegate = { 
    getModelMixins : function(properties) { 

    } 
}; 

var marshaler = new qx.data.marshal.Json(delegate); 
marshaler.toClass(data); 
var model = marshaler.toModel(data); 
// the problem with weeks is that someone changes the dates 
// we must make the getFromDay to return a copy of the data 
weeks.append(qx.data.marshal.Json.createModel(weeksRaw)); 

我該怎麼做?

回答

0

我已經用mixin創建了一個委託。

// notice the _ variables 
weeksRaw.push({ 
    label: "week: " + weeksNum + ' [' + startDayString + '-' + endDayString + '[' 
    ,_fromDay: week.startDay 
    ,_toDay: week.endDay 
}); 
var delegate = { 
    getModelMixins : function(properties) { 
     return myapp.models.marshalMixins.Week; 
    } 
}; 
var marshaler = new qx.data.marshal.Json(delegate); 
marshaler.toClass(weeksRaw); 
var model = marshaler.toModel(weeksRaw); 

的混入:

qx.Mixin.define('production.models.marshalMixins.Week', { 
    members: { 
     getFromDay: function(){ 
      return new Date(this.get_fromDay()); 
     } 
     ,getToDay: function(){ 
      return new Date(this.get_toDay()); 
     } 
    } 

}); 
相關問題