重复更改时,NativeScript UI不响应Observable的propertyChange

时间:2016-04-10 19:17:47

标签: observable nativescript

它必须是我监督的东西,但是当我第一次按预期更新UI时,我更改了Observable的属性(它是一个对象)。但是第二次看到没有变化虽然observable已经改变并且发送了一个通知(我在receiveNotification函数中看到了这一点)。 请参阅下面的代码,通过对话框和unitTap函数进行更改。我在这里缺少什么?

<Page.actionBar>
    <ActionBar title="Products">
        <NavigationButton text="go back" android.systemIcon = "ic_menu_back" tap="goBack"/>
        <ActionBar.actionItems>
            <ActionItem text="Save" position="right" tap="save"/>
        </ActionBar.actionItems>
    </ActionBar>
</Page.actionBar>
<StackLayout>
    <GridLayout cols="*" rows="auto, auto">
        <Label text="productnaam: " cssClass="field-title" />
        <TextField text="{{ product.productName }}" cssClass="field" row="1" col="1" />
    </GridLayout>
    <GridLayout cols="*" rows="auto, auto">
        <Label text="beschrijving: " cssClass="field-title" />
        <TextView text="{{ product.description }}" cssClass="field" row="1" col="1" />
    </GridLayout>
    <GridLayout cols="*" rows="auto, auto">
        <Label text="Prijs per eenheid: " cssClass="field-title"/>
        <TextField text="{{ product.unitPrice }}" cssClass="field" row="1" col="1"/>
    </GridLayout>
    <GridLayout cols="*" rows="auto, auto">
        <Label text="eenheid: " col="0" cssClass="field-title"/>
        <TextField text="{{ product.unit.unit }}" cssClass="field" row="1" col="1" editable="false" tap="unitTap"/>
    </GridLayout>
    <GridLayout cols="*" rows="auto, auto">
        <Label text="btw percentage: " cssClass="field-title"/>
        <TextField text="{{ product.vatPercentage.vatPercentage }}" cssClass="field" row="1" col="1" tap="showModal"/>
    </GridLayout>
</StackLayout>

indexPath.section

2 个答案:

答案 0 :(得分:0)

您从nav-context获得的 var string_after_last_slash = string.substring(start+1); console.log(string_after_last_slash);//o/p:- var3 对象是否可能不是可观察的对象。我可以看到您正在为product创建Observable,但这也不会自动使其所有字段都可观察到。 希望有所帮助。

答案 1 :(得分:0)

好吧,我是我自己的橡皮鸭......

看看这两个例子,看看令人震惊的差异:

obs.xml版本1:

<Page loaded="loaded">
    <StackLayout>
    <TextField text="{{ item.seconds }}"/>
    <TextField text="{{ item.secondsobject.seconds}}"/>
    <Button text="Increase" tap="increaseSeconds"/>
    </StackLayout>
</Page>

obs.js版本1:

var Observable = require("data/observable").Observable;

var pageData = new Observable({
    item: new Observable({seconds: 1, secondsobject: {seconds: 1}})
});

var page;
exports.loaded = function(args) {
    page = args.object;
    page.bindingContext = pageData;
};

exports.increaseSeconds = function() {
    pageData.item.set("seconds", pageData.item.seconds + 1);
    var newValue = pageData.item.secondsobject.seconds + 1;
    pageData.item.set("secondsobject",{seconds: newValue});
};

obs.xml版本2:

<Page loaded="loaded">
    <StackLayout>
    <TextField text="{{ item.seconds }}"/>
    <TextField text="{{ item.secondsobject.secondsobject}}"/>
    <Button text="Increase" tap="increaseSeconds"/>
    </StackLayout>
</Page>

obs.js第2版:

var Observable = require("data/observable").Observable;

var pageData = new Observable({
    item: new Observable({seconds: 1, secondsobject: {secondsobject: 1}})
});

var page;
exports.loaded = function(args) {
    page = args.object;
    page.bindingContext = pageData;
};

exports.increaseSeconds = function() {
    pageData.item.set("seconds", pageData.item.seconds + 1);
    var newValue = pageData.item.secondsobject.secondsobject + 1;
    pageData.item.set("secondsobject",{secondsobject: newValue});
};

版本1按预期工作,每个按钮单击时都会使用新值更新这两个字段。版本2仅在第一次单击时更新字段'item.secondsobject.secondsobject',并忽略连续点击的更新值。 显然,Nativescript的数据绑定不能正确处理具有属性的对象,而属性又具有相同名称的属性......

相关问题