2013-01-22 33 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()); 
     } 
    } 

}); 
相关问题