简体   繁体   English

SAPUI5:将控制器属性绑定到视图

[英]SAPUI5: Binding controller properties to the view

I want to bind properties from my controller to the view. 我想将控制器的属性绑定到视图。

in angularjs it's as simple as manipulating the $scope: 在angularjs中,就像操作$ scope一样简单:

$scope.buttonProps = {
    value: 'cheese',
    disabled: 'disabled',
    onClick: function() {}
};

<custom-button disabled="buttonProps.disabled" onclick="buttonProps.onClick" value="{{buttonProps.value}}" />

however when I try to do the same in SAPUI5 但是,当我尝试在SAPUI5中执行相同操作时

sap.ui.core.mvc.Controller.extend('...', {
    buttonProps: {
        value: 'cheese',
        disabled: 'disabled',
        onClick: function() {}
    }
});

<custom-button value="{buttonProps.value}" disabled="{buttonProps.disabled}" click="buttonProps.onClick" />

it does not work - the framework doesn't seem to be able to access the properties of the "buttonProps" object. 它不起作用-框架似乎无法访问“ buttonProps”对象的属性。

if I, however, move the properties directly onto the controller, it works 但是,如果我将属性直接移动到控制器上,它将起作用

sap.ui.core.mvc.Controller.extend('...', {
    value: 'cheese',
    disabled: 'disabled',
    onClick: function() {}
});

<custom-button value="{value}" disabled="{disabled}" click="onClick" />

but of course, this is a very disorganised way of working when you get more complex views. 但是,当然,当您获得更复杂的视图时,这是一种非常杂乱的工作方式。

I tried creating a JSONModel and binding the values via the model: 我尝试创建一个JSONModel并通过模型绑定值:

sap.ui.core.mvc.Controller.extend('...', {
    buttonProps: new sap.ui.model.json.JSONModel({
        value: 'cheese',
        disabled: 'disabled',
        onClick: function() {}
    })

    onAfterRendering: function() {
        this.byId('btn').setModel(this.buttonProps);
    }
});

<custom-button id="btn" value="{/value}" disabled="{/disabled}" click="/onClick" />

and it works for everything... except for functions. 它适用于所有功能...除了功能。

is there a way for me to bind properties from my controller to the view? 有没有办法将控制器的属性绑定到视图?

UI5 follows the MVC paradigm and event handler is not part of data model. UI5遵循MVC范例,并且事件处理程序不是数据模型的一部分。

The correct way to do data binding is as following: 进行数据绑定的正确方法如下:

View: 视图:

The onClick is defined as a function in the controller not in the data model. onClick被定义为控制器中的函数,而不是数据模型中的函数。

<custom-button id="btn" value="{/value}" disabled="{/disabled}" click="onClick" />

Controller: 控制器:

sap.ui.core.mvc.Controller.extend('...', {

    buttonProps: new sap.ui.model.json.JSONModel({
        value: 'cheese',
        disabled: 'disabled'
    }),

    onInit: function() {
        this.byId('btn').setModel(this.buttonProps);
    },

    onClick:function(oEvent) {

    }    

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM