未捕获的错误:XXX的实例的属性imageWidth尚未准备好

时间:2013-05-23 02:42:01

标签: qooxdoo

我有一个扩展qx.ui.basic.Image的自定义类,这里是代码:

/*
#asset(path/to/*)
*/
qx.Class.define("myApp.myImage",
{
  extend : qx.ui.basic.Image,
  construct: function() {
    this.base(arguments);
    var imgPath = "path/to/test.png";
    this.setSource(imgPath);
    this.imageWidth = qx.util.ResourceManager.getInstance().getImageWidth(imgPath);
  },
  properties : {
    imageWidth : { check : 'Integer' }
  }
});

当我尝试使用时:

var img = new myApp.myImage();
console.log(img.getImageWidth());

它说,

  

未捕获错误:myApp.myImage实例的属性imageWidth   还没准备好

我只想获得图像的图像宽度。我也尝试过使用:

var img = new myApp.myImage();
console.log(img.getBounds().width);

出现的错误是:

  

无法获得未定义的宽度

我错过了什么?

1 个答案:

答案 0 :(得分:1)

让我们来看看属性描述。它没有init值也没有可空。这意味着,当你创建它时,它处于某种未知状态,没有任何价值。像在示例中一样查询该属性会导致错误,因为属性系统不知道要返回什么值,因为您没有指定。 所以这一切都归结为构造函数中的最后一行,在该行中,您将名为“imageWidth”的成员变量设置为图像的右侧宽度。但这不是财产,它是一个普通的成员。因此,要么像img.imageWidth那样访问示例中的成员变量,要么在构造函数中使用setter作为属性:this.setImageWidth(...)