Nativescript Observable不绑定

时间:2016-06-17 07:46:20

标签: nativescript

我正在尝试将图像列表的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的一部分,定义如下:

  

可观测

mainObservable = new Observable({
    currentActive:"explore",
    menuItemsArray:[
        new MenuItem("explore"),
        new MenuItem("community"),
        new MenuItem("profile"),
        new MenuItem("ranking"),
        new MenuItem("camera")
    ]
});

menuItemsArrayMenuItem个对象的数组,它有2个属性:namevisibility,后者绑定到图像。

修改 我特此发布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?

2 个答案:

答案 0 :(得分:2)

更改可见性时,应使用observable的set方法,以便正确通知绑定对象进行更改。所以你应该使用:

mainObservable.menuItemsArray[0].set("visibility", "visible");

答案 1 :(得分:0)

以下是我对你的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;