我試圖將圖像列表的visibility
屬性綁定到數組。Nativescript Observable not binding
以下是我有:
XML標記
<GridLayout id="menu">
<Image src="{{ ... }}" tap="menuItemTap" id="explore" visibility="{{ menuItemsArray[0].visibility }}" />
<Image src="{{ ... }}" tap="menuItemTap" id="community" visibility="{{ menuItemsArray[1].visibility }}"/>
<Image src="{{ ... }}" tap="menuItemTap" id="profile" visibility="{{ menuItemsArray[2].visibility }}"/>
<Image src="{{ ... }}" tap="menuItemTap" id="ranking" visibility="{{ menuItemsArray[3].visibility }}"/>
<Image src="{{ ... }}" tap="menuItemTap" id="camera" visibility="{{ menuItemsArray[4].visibility }}"/>
</GridLayout>
正如你所看到的,我結合visibility
到menuItemsArray
,這是可觀察到的定義爲以下的部分:
OBSERVABLE
mainObservable = new Observable({
currentActive:"explore",
menuItemsArray:[
new MenuItem("explore"),
new MenuItem("community"),
new MenuItem("profile"),
new MenuItem("ranking"),
new MenuItem("camera")
]
});
menuItemsArray
是MenuItem
對象,其具有2個屬性的數組:name
和visibility
,後者被綁定到的圖像。
編輯 在此,我也張貼MenuItem
類
菜單項
var menuitem = function(name,visibility){
var _name = name;
var _visibility = visibility || "collapsed" ;
Object.defineProperty(this,"visibility",{
get : function(){
return _visibility;
},
set : function(visibility){
_visibility = visibility;
console.log("called set-> visibility to " + _visibility);
}
});
Object.defineProperty(this,"name",{
get : function(){
return _name;
},
set : function(name){
_name = name;
}
})
}
按@ peterstaev的建議,我也試過如下:
菜單項
class MenuItem extends Observable{
constructor(name,visibility){
super();
this._name = name;
this._visibility = visibility || "collapsed";
}
get visibility(){
return this._visibility;
}
set visibility(visibility){
this._visibility = visibility;
console.log("called set-> visibility to " + this._visibility);
}
get name(){
return this._name;
}
set name(name){
this._name = _name;
}
};
module.exports = MenuItem;
有趣visibility
設置爲"collapsed"
每默認情況下,並且,事實上,沒有任何圖像出現(這是所希望的行爲)。但是,如果切換visibility
到"visible"
- mainObservable.menuItemsArray[0].visibility = "visible"
- 它不會影響圖像,因爲它不會顯示。 是什麼導致這個問題?我不正確地使用Observable嗎?
都能跟得上不工作:(無論如何謝謝你! –
@AaronUllal:嘗試'mainObservable.set(「menuItemsArray [0] .visibility」,「visible」);'也可能需要mak e你的'MenuItem'類擴展'Observable'。 –
我將'MenuItem'擴展爲Observable。但是 'mainObservable.menuItemsArray [0] .set(visibility,「visible」);'throws'visibility is not defined'和 'mainObservable。menuItemsArray [0] .visibility =「visible」;'不會拋出錯誤,但不會使圖像可見 –