Nativescript Observable 不绑定
Nativescript Observable not binding
我正在尝试将图像列表的 visibility
属性 绑定到数组。
这是我的资料:
XML MARKUP
<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 的一部分:
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
,后者绑定到图像。
编辑
我特此也postMenuItem
class
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 的建议,我还尝试了以下操作:
MenuItem
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"
,事实上,显示 none 的图像(这是期望的行为)。但是,如果将 visibility
切换为 "visible"
- mainObservable.menuItemsArray[0].visibility = "visible"
- 它不会影响图像,因为它不会显示。
是什么导致了这个问题?我是否错误地使用了 Observable?
当您更改可见性时,您应该使用可观察对象的 set
方法,以便它正确通知绑定对象的更改。所以你应该使用:
mainObservable.menuItemsArray[0].set("visibility", "visible");
这是我对你的 senario 的处理方法:
在page.xml
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo">
<DockLayout id="menu">
<ListView items="{{ menuItems }}" loaded="onLoaded" itemLoading="onItemLoading" itemTap="onItemTap">
<ListView.itemTemplate>
<StackLayout>
<Image src="res://icon" stretch="none" visibility="{{ $value.visibility ? 'visible' : 'collapsed' }}" />
</StackLayout>
</ListView.itemTemplate>
</ListView>
</DockLayout>
</Page>
在page.js
"use strict";
var observable_1 = require("data/observable");
var observable_array_1 = require("data/observable-array");
var MenuItem = (function (_super) {
__extends(MenuItem, _super);
function MenuItem(name) {
_super.call(this);
this._name = name;
this._visibility = false;
}
Object.defineProperty(MenuItem.prototype, "name", {
get: function () {
return this._name;
},
set: function (value) {
this._name = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MenuItem.prototype, "visibility", {
get: function () {
return this._visibility;
},
set: function (value) {
this._visibility = value;
this.notifyPropertyChange("isVisible", value);
},
enumerable: true,
configurable: true
});
return MenuItem;
}(observable_1.Observable));
var itemsObservableArray = new observable_array_1.ObservableArray([
new MenuItem("explore"),
new MenuItem("community"),
new MenuItem("profile"),
new MenuItem("ranking"),
new MenuItem("camera")
]);
var viewModel = new observable_1.Observable({
currentActive: "explore",
menuItems: itemsObservableArray
});
function navigatingTo(args) {
var page = args.object;
itemsObservableArray.getItem(0).set("visibility", true);
itemsObservableArray.getItem(1).set("visibility", true);
itemsObservableArray.getItem(2).set("visibility", true);
page.bindingContext = viewModel;
}
exports.navigatingTo = navigatingTo;
我正在尝试将图像列表的 visibility
属性 绑定到数组。
这是我的资料:
XML MARKUP
<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 的一部分:
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
,后者绑定到图像。
编辑
我特此也postMenuItem
class
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 的建议,我还尝试了以下操作:
MenuItem
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"
,事实上,显示 none 的图像(这是期望的行为)。但是,如果将 visibility
切换为 "visible"
- mainObservable.menuItemsArray[0].visibility = "visible"
- 它不会影响图像,因为它不会显示。
是什么导致了这个问题?我是否错误地使用了 Observable?
当您更改可见性时,您应该使用可观察对象的 set
方法,以便它正确通知绑定对象的更改。所以你应该使用:
mainObservable.menuItemsArray[0].set("visibility", "visible");
这是我对你的 senario 的处理方法:
在page.xml
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo">
<DockLayout id="menu">
<ListView items="{{ menuItems }}" loaded="onLoaded" itemLoading="onItemLoading" itemTap="onItemTap">
<ListView.itemTemplate>
<StackLayout>
<Image src="res://icon" stretch="none" visibility="{{ $value.visibility ? 'visible' : 'collapsed' }}" />
</StackLayout>
</ListView.itemTemplate>
</ListView>
</DockLayout>
</Page>
在page.js
"use strict";
var observable_1 = require("data/observable");
var observable_array_1 = require("data/observable-array");
var MenuItem = (function (_super) {
__extends(MenuItem, _super);
function MenuItem(name) {
_super.call(this);
this._name = name;
this._visibility = false;
}
Object.defineProperty(MenuItem.prototype, "name", {
get: function () {
return this._name;
},
set: function (value) {
this._name = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MenuItem.prototype, "visibility", {
get: function () {
return this._visibility;
},
set: function (value) {
this._visibility = value;
this.notifyPropertyChange("isVisible", value);
},
enumerable: true,
configurable: true
});
return MenuItem;
}(observable_1.Observable));
var itemsObservableArray = new observable_array_1.ObservableArray([
new MenuItem("explore"),
new MenuItem("community"),
new MenuItem("profile"),
new MenuItem("ranking"),
new MenuItem("camera")
]);
var viewModel = new observable_1.Observable({
currentActive: "explore",
menuItems: itemsObservableArray
});
function navigatingTo(args) {
var page = args.object;
itemsObservableArray.getItem(0).set("visibility", true);
itemsObservableArray.getItem(1).set("visibility", true);
itemsObservableArray.getItem(2).set("visibility", true);
page.bindingContext = viewModel;
}
exports.navigatingTo = navigatingTo;